Esempio n. 1
0
 public static async Task<PingReply> Ping(string hostNameOrIPAddress, int ttl = 0)
 {
     var pinger = new Ping();
     var options = new PingOptions(ttl, dontFragment: true);
     
     return await pinger.SendTaskAsync(hostNameOrIPAddress, 0x1388, DefaultSendBuffer, options);
 }
        private async Task<ICMPResult> ICMPTest(Server server)
        {
            Logging.Debug("Ping " + server.FriendlyName());
            if (server.server == "") return null;
            var result = new ICMPResult(server);
            try
            {
                var IP =
                    Dns.GetHostAddresses(server.server)
                        .First(
                            ip =>
                                ip.AddressFamily == AddressFamily.InterNetwork ||
                                ip.AddressFamily == AddressFamily.InterNetworkV6);
                var ping = new Ping();

                foreach (var _ in Enumerable.Range(0, Repeat))
                {
                    try
                    {
                        var reply = await ping.SendTaskAsync(IP, TimeoutMilliseconds);
                        if (reply.Status.Equals(IPStatus.Success))
                        {
                            result.RoundtripTime.Add((int?) reply.RoundtripTime);
                        }
                        else
                        {
                            result.RoundtripTime.Add(null);
                        }

                        //Do ICMPTest in a random frequency
                        Thread.Sleep(TimeoutMilliseconds + new Random().Next()%TimeoutMilliseconds);
                    }
                    catch (Exception e)
                    {
                        Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
                        Logging.LogUsefulException(e);
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
                Logging.LogUsefulException(e);
            }
            return result;
        }
 private async Task<List<DataList>> ICMPTest(Server server)
 {
     Logging.Debug("Ping " + server.FriendlyName());
     if (server.server == "") return null;
     var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6));
     var ping = new Ping();
     var ret = new List<DataList>();
     foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern)))
     {
         //ICMP echo. we can also set options and special bytes
         try
         {
             var reply = await ping.SendTaskAsync(IP, Timeout);
             ret.Add(new List<KeyValuePair<string, string>>
             {
                 new KeyValuePair<string, string>("Timestamp", timestamp),
                 new KeyValuePair<string, string>("Server", server.FriendlyName()),
                 new KeyValuePair<string, string>("Status", reply?.Status.ToString()),
                 new KeyValuePair<string, string>("RoundtripTime", reply?.RoundtripTime.ToString())
                 //new KeyValuePair<string, string>("data", reply.Buffer.ToString()); // The data of reply
             });
             Thread.Sleep(Timeout + new Random().Next() % Timeout);
             //Do ICMPTest in a random frequency
         }
         catch (Exception e)
         {
             Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
             Logging.LogUsefulException(e);
         }
     }
     return ret;
 }