This code was created by me for my work to ping our Cisco switches. It tells in a percentage how many packets were lost during each ping.
Ping your server / a server for packet loss. If more than X% then the server/network is down.Code:system ("c:\windows\system32\ping.exe");
Below is a very simple program that I wrote to determine if any of our network switches are down.
Modify this string: hostArray[0] = "127.0.0.1"; to your server IP. This method is extremely fast and to make it faster reduce the amount of loops. I've also attached the complete program. This uses the tcp.PingReply class. Modify as needed.Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.NetworkInformation; namespace PacketLoss { public partial class Form1 : Form { // Variables String [] hostArray; int amt_ping; public Form1() { InitializeComponent(); // Declare the array and give it values hostArray = new String[14]; // Set our ping amt amt_ping = 6; // Enter our hosts hostArray[0] = "127.0.0.1"; } /// <summary> /// This function pings all of the hosts in the hostArray /// </summary> private void ping_hosts() { // 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 } // End foreach // Update our label lbl_status.Text = "Complete - press Ping to restart."; // Enable our button btn_ping.Enabled = true; } /// <summary> /// Pings IP address and shows packet loss /// </summary> /// /// private void btn_ping_Click(object sender, EventArgs e) { ping_hosts(); } /// <summary> /// Retrieves the packet loss in a percentage /// </summary> /// /// <returns></returns> 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; /*tb_results.Text = "Address: " + reply.Address.ToString(); Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime); Console.WriteLine("Time to live: {0}", reply.Options.Ttl); Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); Console.WriteLine("Buffer size: {0}", reply.Buffer.Length); */ } // Allow the GUI to update Application.DoEvents(); } // End For // Return the percentage double percent = (failed / pingAmount) * 100; return percent; } /// <summary> /// When the form loads start the ping /// </summary> /// /// private void Form1_Load(object sender, EventArgs e) { } private void Form1_Shown(object sender, EventArgs e) { ping_hosts(); } } }
Last edited by Jordan; 03-13-2008 at 05:16 AM.
cool
cool![]()
Thanks![]()
I dont see the definition of your Ping class and i have to post before i can download your source.
The Ping class is part of .NET in System.Net.NetworkInformation.
Ping Class (System.Net.NetworkInformation)
where can i ping my addresses every time i change host array it always pings 10.225..... and it shows that there is an error in lv_result.systemindex changed it says that it should exist after += where it is found there could u help me plz urgent
will try it out
thx .. i will look into it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks