Exemple #1
0
        static int Main(string[] args)
        {
            Ping        pingSender = new Ping();
            PingOptions options    = new PingOptions();
            // Create  the ping target object, aka pt
            PingerTarget pt      = new PingerTarget();
            bool         verbose = false;  // true = print additional versbose stuff for the program
            //bool loop = true; // true = ping will loop until Ctrl + C is pressed
            int    items             = -1; // compensate for "pinger" counting as 1 command line argument
            int    maxcount          = 0;
            bool   maxCountSpecified = false;
            bool   smartping         = true;                // by default use the smart ping switch
            bool   return_code_only  = false;
            string target            = "";                  // target IP address or DNS name to ping
            int    defaultPollingTimeInMilliseconds = 1000; //iteration defaultPollingTimeInMilliseconds in ms or can be seen as polling
            bool   stopBeeps         = false;
            bool   outputScreenToCSV = false;               // Output only what's on the screen to CSV. So if printing only changes then it will only output to file that
            bool   outputAllToCSV    = false;               // Output every ping response to CSV, even if you are using the smart ping function which only prints the status changes
            string outputCSVFilename = "";
            string outstr            = "";
            int    sleeptime         = defaultPollingTimeInMilliseconds;
            int    sleeptimesec      = sleeptime / 1000;
            double timelaps          = 0;
            int    runtimeError      = 0;
            int    runtimeInHours    = 0;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

            byte[] buffer     = Encoding.ASCII.GetBytes(data);
            int    timeout    = 1000; // amount of time in ms to wait for the ping reply
            int    timeoutsec = timeout / 1000;

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.

            // iterate through the arguments
            string[] arguments = Environment.GetCommandLineArgs();
            for (int argIndex = 0; argIndex < arguments.Length; argIndex++)
            {
                //logThis("Arguments " + arg);
                switch (arguments[argIndex].ToUpper())
                {
                case "/?":
                    ShowSyntax();
                    runtimeError = 1;
                    break;

                case "-R":     // Returns code only, doesn't expects a value after this switch
                    return_code_only  = true;
                    maxcount          = 1;
                    maxCountSpecified = true;
                    break;

                case "-V":
                    verbose = true;
                    break;

                case "-S":     // Make pinger like ping and output every responses to screen
                    smartping = false;
                    break;

                case "-N":     // No loop, same as using '-c 1'
                    maxcount          = 1;
                    maxCountSpecified = true;
                    //loop = false;
                    break;

                case "-Q":     // quietens audible sounds (beeps)
                    stopBeeps = true;
                    break;

                case "-C":     // Specify how many times pinger will loop ping a host, expects a positive value after the switch equal or greater than 1
                    try
                    {
                        argIndex++;     // get the next value, hopefully a digit
                        //bool success = int.TryParse(arguments[argIndex], out sleeptime);
                        maxCountSpecified = true;
                        maxcount          = int.Parse(arguments[argIndex]);
                    }
                    catch (System.ArgumentNullException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.FormatException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.OverflowException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    break;

                case "-H":     // Run pinger for a number of hours, expects a positive value after the switch
                    try
                    {
                        argIndex++;     // get the next value, hopefully a digit
                        //bool success = int.TryParse(arguments[argIndex], out sleeptime);
                        runtimeInHours    = int.Parse(arguments[argIndex]);
                        maxCountSpecified = false;
                        //loop = true;
                    }
                    catch (System.ArgumentNullException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.FormatException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.OverflowException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        logThis("Please specify a valid number.");
                        runtimeError = 1;
                    }
                    break;

                case "-CSV":     // Output each ping responses to a CSV file but matches onscreen output.
                    //verbose = false;
                    outputScreenToCSV = true;
                    outputAllToCSV    = !outputScreenToCSV;
                    break;

                case "-CSVALL":     // Output each ping responses to a CSV file even if you are using the smartping function
                    //verbose = false;
                    outputScreenToCSV = false;
                    outputAllToCSV    = !outputScreenToCSV;
                    break;

                case "-P":     // Poll every 'n' seconds, expects a value after this switch
                    try
                    {
                        argIndex++;     // get the next value, hopefully a digit
                        //bool success = int.TryParse(arguments[argIndex], out sleeptime);
                        sleeptime = int.Parse(arguments[argIndex]) * 1000;
                    }
                    catch (System.ArgumentNullException)
                    {
                        logThis("Please specify a valid polling interval in seconds.");
                        runtimeError = 1;
                    }
                    catch (System.FormatException)
                    {
                        logThis("Please specify a valid polling interval in seconds.");
                        runtimeError = 1;
                    }
                    catch (System.OverflowException)
                    {
                        logThis("Please specify a valid polling interval in seconds.");
                        runtimeError = 1;
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        logThis("Please specify a valid polling interval in seconds.");
                        runtimeError = 1;
                    }
                    break;

                case "-T":     // smart switch
                    // Show OK and Down and OK, implies -C
                    try
                    {
                        argIndex++;     // get the next value, hopefully a digit
                        timeout = int.Parse(arguments[argIndex]) * 1000;
                    }
                    catch (System.ArgumentNullException)
                    {
                        logThis("Please specify a valid timeout value in seconds larger than 1 seconds.");
                        runtimeError = 1;
                    }
                    catch (System.FormatException)
                    {
                        logThis("Please specify a valid timeout value in seconds larger than 1 seconds.");
                        runtimeError = 1;
                    }
                    catch (System.OverflowException)
                    {
                        logThis("Please specify a valid timeout value in seconds larger than 1 seconds.");
                        runtimeError = 1;
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        logThis("Please specify a valid timeout value in seconds larger than 1 seconds.");
                        runtimeError = 1;
                    }
                    break;

                default:
                    if (items == 0)
                    {
                        target = arguments[argIndex];
                        //smartping = true;
                    }
                    items++;
                    break;
                }
            }

            if (runtimeError > 0)
            {
                return(runtimeError);
            }

            if (items > 1 || target.Length <= 0)
            {
                ShowSyntax();
                runtimeError = 1;
            }
            else
            {
                pt.Target    = target;
                pt.Startdate = DateTime.Now;

                string[] dnsresults = DNSLookup(pt.Target);

                if (dnsresults.Length > 0)
                {
                    pt.Hostname        = dnsresults[0];
                    pt.IPAddress       = dnsresults[1];
                    pt.DNSLookupStatus = dnsresults[2];
                }
                else
                {
                    pt.Hostname = pt.Target;
                }


                if (!return_code_only)
                {
                    logThis("Pinging " + pt.Target + " at " + sleeptimesec + "sec interval & timeout of " + timeoutsec + " seconds");
                    logThis("Looking up DNS : ");
                    logThis("      Hostname : " + pt.Hostname);
                    logThis("      IPAddress: " + pt.IPAddress);
                    logThis("");
                    if (!maxCountSpecified && runtimeInHours > 0)
                    {
                        maxcount = (runtimeInHours * 60 * 60) / (sleeptimesec);
                        //logThis(">> sleeptime = " + sleeptimesec + ",runtimeInHours=" + runtimeInHours + "hrs,maxcount=" + maxcount +"<<");
                        logThis(">> Runtime: " + runtimeInHours + "hrs, Total ping expected=" + maxcount + " <<");
                    }
                }
                if (outputScreenToCSV || outputAllToCSV)
                {
                    outputCSVFilename = "pinger-" + pt.Target.Replace('.', '_').Trim() + "_" + ".txt";
                    logThis(">> Responses will be saved to " + outputCSVFilename);
                    logThis("");
                    logToFile("Date,TimeLaps (sec),Target,Reply,Round Trip (ms),TTL,Count", outputCSVFilename);
                }
                if (!return_code_only)
                {
                    logThis("Date,TimeLaps (sec),Target,Reply,Round Trip (ms),TTL,Count");
                }
                do
                {
                    pt.CurrStatusPingDate = DateTime.Now;
                    pt.CurrHostPingCount++;
                    try
                    {
                        options.DontFragment = true;
                        PingReply reply;

                        if (pt.IPAddress != null)
                        {
                            reply = pingSender.Send(pt.IPAddress, timeout, buffer, options);
                            //pt.CurrHostPingCount++; // redundant with the one below ? should I take it outside this if else..may..no time to test it thought.
                        }
                        else
                        {
                            reply = pingSender.Send(pt.Hostname, timeout, buffer, options);
                            //pt.CurrHostPingCount++; // redundant with the one one ? should I take it outside this if else..may..no time to test it thought.
                        }

                        if (reply != null && reply.Options != null)
                        {
                            pt.OptionsTtl = reply.Options.Ttl;
                        }
                        else
                        {
                            pt.OptionsTtl = -1;
                        }

                        pt.ReplyIPAddress     = reply.Address.ToString(); // ? reply.Address.ToString() : "-";
                        pt.RoundTrip          = reply.RoundtripTime;      //? reply.RoundtripTime : -1);
                        pt.CurrHostPingStatus = reply.Status.ToString();  //? reply.Status.ToString() : "-");
                        // logThis(reply.Status.ToString());
                        if (reply.Status.ToString() == "DestinationHostUnreachable")
                        {
                            pt.Errorcode          = 1;
                            pt.CurrHostPingStatus = "DestinationHostUnreachable";
                        }
                        else
                        {
                            pt.Errorcode = 0;
                        }
                    }
                    catch (System.Net.Sockets.SocketException se)
                    {
                        pt.Errorcode = 1; pt.ErrorMsg = se.Message; pt.CurrHostPingStatus = se.Message; Thread.Sleep(sleeptime);
                    }
                    catch (System.Net.NetworkInformation.PingException pe)
                    {
                        pt.Errorcode = 1; pt.ErrorMsg = pe.Message; pt.CurrHostPingStatus = pe.Message; Thread.Sleep(sleeptime);
                    }
                    catch (System.NullReferenceException nre)
                    {
                        pt.Errorcode = 1; pt.ErrorMsg = nre.Message; pt.CurrHostPingStatus = "DestinationHostUnreachable"; Thread.Sleep(sleeptime);
                        //pt.Errorcode = 1; pt.ErrorMsg = nre.Message; pt.CurrHostPingStatus = nre.Message; Thread.Sleep(sleeptime);
                    }
                    finally
                    {
                        if (pt.Errorcode == 0)
                        {
                            pt.HostReachableCountUpdate++;
                            // timelaps = (sleeptimesec) * (pt.CurrHostPingCount - pt.PrevStatusPingCount);
                        }
                        else if (pt.Errorcode == 1)
                        {
                            pt.HostUnreachableCountUpdate++;
                            // timelaps = (sleeptimesec) * (pt.CurrHostPingCount - pt.PrevStatusPingCount);timelaps = (sleeptimesec + timeoutsec) * (pt.CurrHostPingCount - pt.PrevStatusPingCount);
                        }
                        else
                        {
                            logThis("Unknown ping error code");
                        }

                        TimeSpan difference = pt.CurrStatusPingDate.Subtract(pt.PrevStatusPingDate);
                        timelaps = Math.Ceiling(difference.TotalSeconds);
                        // Create the output string
                        if (!verbose)
                        {
                            outstr = pt.CurrStatusPingDate + "," + timelaps + "sec," + pt.Hostname + "," + pt.CurrHostPingStatus + "," + pt.RoundTrip + "ms," + pt.OptionsTtl + "," + pt.CurrHostPingCount;
                        }
                        else
                        {
                            outstr = "Date=" + pt.CurrStatusPingDate + ",TimeLapsSec=" + timelaps + "sec,trgt=" + pt.Hostname + ",status=" + pt.CurrHostPingStatus + ",rndtrip=" + pt.RoundTrip + "ms,ttl=" + pt.OptionsTtl + ",count=" + pt.CurrHostPingCount;
                        }

                        if (pt.PrevHostPingStatus != pt.CurrHostPingStatus)
                        {
                            pt.PrevStatusPingDate = pt.CurrStatusPingDate;
                            // 1 print to screent the difference
                            if (!return_code_only)
                            {
                                logThis(outstr);
                            }
                            // 2 - write to log file if requested to
                            if (outputScreenToCSV || outputAllToCSV)
                            {
                                logToFile(outstr, outputCSVFilename);
                            }

                            if (pt.Errorcode == 0 && pt.CurrHostPingCount > 1 && !stopBeeps)
                            {
                                for (int i = 0; i < 2; i++)
                                {
                                    Console.Beep();
                                }
                            }
                            else if (pt.Errorcode == 1 && pt.CurrHostPingCount > 1 && !stopBeeps) //&& smartping
                            {
                                for (int i = 0; i < 4; i++)
                                {
                                    Console.Beep();
                                }
                            }
                            pt.PrevStatusPingCount = pt.CurrHostPingCount;
                        }
                        else // At this point the current and previous ping status differ so we need to process
                        {
                            // Only output to screen if the smart ping is not enabled and pinger behaves like ping
                            if (!smartping)
                            {
                                if (!return_code_only)
                                {
                                    logThis(outstr);
                                }
                            }
                            // Only output to screen if the
                            if (outputAllToCSV)
                            {
                                logToFile(outstr, outputCSVFilename);
                            }
                        }
                    }
                    if (!maxCountSpecified)
                    {
                        maxcount = pt.CurrHostPingCount + 1;
                    }
                    if (verbose)
                    {
                        pt.Printout();
                    }
                    //if (loop) { Thread.Sleep(sleeptime); }
                    if (pt.CurrHostPingCount < maxcount)
                    {
                        Thread.Sleep(sleeptime);
                    }
                } while (pt.CurrHostPingCount < maxcount);
                //while (loop && pt.CurrHostPingCount < maxcount) ;
            }
            //logThis(pt.Errorcode.ToString());
            return(pt.Errorcode);
        }
Exemple #2
0
 static private void PingAll(List <string> ips)
 {
     ips.AsParallel().Select(ip => { Console.WriteLine("Try " + ip); Ping ping = new Ping(); ping.PingCompleted += Ping_PingCompleted; ping.SendAsync(ip, 2000);  return(string.Empty); });
 }