示例#1
0
    static void Main(string[] args)
    {
        System.Timers.Timer timer = new System.Timers.Timer(100);         //timer updating progress percentage
        timer.Elapsed += async(sender, e) => await HandleTimer();

        IP  FirstIP = null, LastIP = null;
        int Timeout = 0;

        //checking if any arguments were passed
        if (args != null)
        {
            IP.TryParse((from a in args where a.Substring(0, 2) == "-f" select a.Substring(2)).FirstOrDefault(), out FirstIP);
            IP.TryParse((from a in args where a.Substring(0, 2) == "-l" select a.Substring(2)).FirstOrDefault(), out LastIP);
            Int32.TryParse((from a in args where a.Substring(0, 2) == "-t" select a.Substring(2)).FirstOrDefault(), out Timeout);
        }
        while (PingCount < 0)       //can continue only when FirstIP is lower than LastIP
        {
            while (FirstIP == null)
            {
                Console.WriteLine("First IP Address: ");
                IP.TryParse(Console.ReadLine(), out FirstIP);
                if (FirstIP == null)
                {
                    Console.WriteLine("Enter correct IP address");
                }
            }

            while (LastIP == null)
            {
                Console.WriteLine("Last IP address: ");
                IP.TryParse(Console.ReadLine(), out LastIP);
                if (LastIP == null)
                {
                    Console.WriteLine("Enter correct IP address");
                }
            }

            PingCount = LastIP - FirstIP;
            if (PingCount < 0)
            {
                Console.WriteLine("First IP address must be lower than last");
                FirstIP = null;
                LastIP  = null;
            }
        }

        while (Timeout <= 0)       //can continue only if Timeout is higher than 0
        {
            Console.WriteLine("Ping timeout [ms]: ");
            Int32.TryParse(Console.ReadLine(), out Timeout);
            if (Timeout <= 0)
            {
                Console.WriteLine("Enter positive integer value");
            }
        }

        statuses = new ConcurrentBag <IPStat>();
        CurPos   = Console.CursorTop;
        timer.Start();

        IP[] addresses = IP.GetArray(FirstIP, LastIP);        //creating array with IPs to ping

        //pinging IPs
        Parallel.ForEach(addresses, ip =>
        {
            Ping pingSender = new Ping();
            PingReply reply = pingSender.Send(ip, Timeout);

            if (reply.Status == IPStatus.Success)
            {
                IPStat ipStat;
                try                 //if reply is recieved, trying to get HostName
                {
                    IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                    ipStat = new IPStat(ip, true, reply.RoundtripTime.ToString(), hostEntry.HostName.ToString());
                }
                catch
                {
                    ipStat = new IPStat(ip, true, reply.RoundtripTime.ToString());
                }
                statuses.Add(ipStat);
            }
            else
            {
                statuses.Add(new IPStat(ip, false));
            }
        });

        var statusesSorted = from s in statuses orderby s.ip select s;         //sorting IPStats by IP

        Console.WriteLine();
        timer.Stop();
        timer.Dispose();
        Console.SetCursorPosition(0, CurPos);
        Console.WriteLine("    ");
        Console.WriteLine(String.Format("{0,-18}{1,-18}{2,-10}{3,-60}", "IP address", "Reply", "Time", "Name")); //table header

        foreach (var s in statusesSorted)                                                                        //building table
        {
            Console.WriteLine(s.ShowStat());
        }

        Console.ReadLine();
    }