Reputation: 41
Let me explain what I am attempting to do with this program. We have 26 different locations on our network. The way our IP addresses are set up varies by location. For our corporate office all ip addresses go 10.1.?.? At another location it would be 10.2.?.? and so on through each location. We then label each PC as a number and the IP Address corresponds with that PC number. So my PC is 10.1.2.98 because I am at the corporate office with PC298. I am building a program that will allow me to input a potential PC number and ping each location to see if that IP is available for that PC number. So for example I would enter 466 and the program would able to ping each location. It would ping 10.1.4.66 then it would ping 10.2.4.66 and so on. I have not be able to find anything similar; so currently I just have the program pinging current IP addresses on the network separated by branches. It takes a long time to ping all of those addresses this way when I really only need to ping 26 addresses to test it. I want to be able to look for open addresses so we can add new PCs to the location without using the same IP address. Any ideas?
Below is my current setup to scanning one of the locations so you can see how I am currently pinging in the program. However, I am aware that it will need to change a lot in order to input user request.
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;
}
}
Upvotes: 0
Views: 1476
Reputation: 116108
Since the number of your IPs to ping is not much, you can create tasks to ping your clients asynchronously.
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();
Upvotes: 2