如何根据用户输入ping一系列ip地址

本文关键字:一系列 ip 地址 ping 输入 何根 用户 | 更新日期: 2023-09-27 18:22:16

让我解释一下我试图用这个程序做什么。我们的网络上有26个不同的位置。我们的IP地址设置方式因位置而异。对于我们的公司办公室,所有ip地址都是10.1。?。?在另一个地方是10.2。?。?等等。然后,我们将每台电脑标记为一个号码,IP地址与该电脑号码相对应。所以我的电脑是10.1.2.98,因为我和PC298在公司办公室。我正在构建一个程序,允许我输入一个潜在的电脑号码,并ping每个位置,看看该IP是否可用于该电脑号码。例如,我会输入466,程序将能够ping每个位置。它会ping 10.1.4.66,然后会ping 10.2.4.66,依此类推。我找不到类似的东西;所以目前我只是让程序ping网络上由分支分隔的当前IP地址。当我真的只需要ping 26个地址来测试时,用这种方式ping所有这些地址需要很长时间。我希望能够查找打开的地址,这样我们就可以在不使用相同IP地址的情况下将新电脑添加到该位置。有什么想法吗?

下面是我当前扫描其中一个位置的设置,这样你就可以看到我当前在程序中的ping方式。然而,我知道它需要做很多更改才能输入用户请求。

public _8thStreet()
    {
        InitializeComponent();
        hostArray = new String[27];
        // Set our ping amt
        amt_ping = 2;
        // Enter our hosts
        hostArray[0] = "10.2.4.49";
        hostArray[1] = "10.2.4.50";
        hostArray[2] = "10.2.4.51";
        hostArray[3] = "10.2.4.52";
        hostArray[4] = "10.2.4.53";
        hostArray[5] = "10.2.4.54";
        hostArray[6] = "10.2.4.55";
        hostArray[7] = "10.2.4.56";
        hostArray[8] = "10.2.4.57";
        hostArray[9] = "10.2.4.58";
        hostArray[10] = "10.2.4.59";
        hostArray[11] = "10.2.4.60";
        hostArray[12] = "10.2.4.61";
        hostArray[13] = "10.2.4.62";
        hostArray[14] = "10.2.4.63";
        hostArray[15] = "10.2.4.64";
        hostArray[16] = "10.2.4.65";
        hostArray[17] = "10.2.4.66";
        hostArray[18] = "10.2.4.67";
        hostArray[19] = "10.2.4.68";
        hostArray[20] = "10.2.4.69";
        hostArray[21] = "10.2.4.70";
        hostArray[22] = "10.2.4.71";
        hostArray[23] = "10.2.4.72";
        hostArray[24] = "10.2.4.73";
        hostArray[25] = "10.2.4.74";
        hostArray[26] = "10.2.4.75";
    }
    private void ping_hosts()
    {
        try
        {
            // Disable our button
            btn_ping.Enabled = false;
            // Clear our list view
            lv_results.Items.Clear();
            // Cycle through our host array
            foreach (String host in hostArray)
            {
                // Write our status
                lbl_status.Text = "Pinging (x" + amt_ping + "): " + host;
                // Allow the GUI to update
                Application.DoEvents();
                // Ping the host four times
                double loss = get_loss(host, amt_ping);
                // Determine if there is any loss
                if (loss > 0)
                {
                    // Insert into the List View
                    ListViewItem lv = lv_results.Items.Insert(lv_results.Items.Count, host);
                    lv.SubItems.Add(Convert.ToString(loss) + "%");
                } // End If
                else
                {
                    //Insert into the List View
                    ListViewItem lv = lv_results.Items.Insert(lv_results.Items.Count, host);
                    lv.SubItems.Add(Convert.ToString(loss) + "%");
                }
            } // End foreach
            // Update our label
            lbl_status.Text = "Complete - press Ping to restart.";
            // Enable our button
            btn_ping.Enabled = true;
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }
    }
    private void btn_ping_Click(object sender, EventArgs e)
    {
        ping_hosts();
    }
    private double get_loss(String host, int pingAmount)
    {
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();
        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;
        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        int failed = 0;
        // Loop the amount of times to ping
        for (int i = 0; i < pingAmount; i++)
        {
            PingReply reply = pingSender.Send(host, timeout, buffer, options);
            if (reply.Status != IPStatus.Success)
            {
                failed += 1;
            }
            // Allow the GUI to update
            Application.DoEvents();
        } // End For
        // Return the percentage
        double percent = (failed / pingAmount) * 100;
        return percent;
    }
}

如何根据用户输入ping一系列ip地址

由于要ping的IP数量不多,因此可以创建异步ping客户端的任务。

string[] ips = new string[] { "192.168.1.1", "192.168.1.50" , "192.168.1.100" };
var pingTasks = ips.Select(address => new Ping().SendTaskAsync(address));
var replies = await Task.WhenAll(pingTasks);
var alives = replies.Where(r => r.Reply.Status == IPStatus.Success)
                    .Select(r=>r.Address)
                    .ToList();