Guest User

Ping Logger

a guest
Oct 24th, 2015
3,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.NetworkInformation;
  7. using System.IO;
  8. using System.Threading;
  9.  
  10. namespace Ping_Logger
  11. {
  12.     class Program
  13.     {
  14.         static string data;
  15.         static byte[] buffer;
  16.         static int timeout;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             Dictionary<string, string> addresses = new Dictionary<string, string>();
  21.             addresses.Add("Router", "192.168.1.1");
  22.             addresses.Add("Modem", "192.168.0.1");
  23.             addresses.Add("ISP", "83.169.183.125");
  24.             addresses.Add("GoogleLocal", "173.194.116.211");
  25.             addresses.Add("GoogleDNS", "8.8.8.8");
  26.             addresses.Add("PublicDNS", "4.2.2.2");
  27.             Ping pingSender = new Ping();
  28.             PingOptions options = new PingOptions();
  29.  
  30.             // Use the default Ttl value which is 128,
  31.             // but change the fragmentation behavior.
  32.             options.DontFragment = true;
  33.  
  34.             data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  35.             buffer = Encoding.ASCII.GetBytes(data);
  36.             timeout = 120;
  37.             // Create a buffer of 32 bytes of data to be transmitted.
  38.             while (true)
  39.             {
  40.                 StreamWriter sw = new StreamWriter(@"C:\Users\OMMITTED\Documents\Results\ping.txt", true);
  41.                 DateTime curTime = DateTime.Now;
  42.                 string newLine = string.Format("{0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString());
  43.                 foreach (var addy in addresses)
  44.                 {
  45.                     newLine += string.Format(",{0}", PingSite(pingSender, options, addy.Value));
  46.                 }
  47.                 sw.WriteLine(newLine);
  48.                 Console.WriteLine(newLine);
  49.                 sw.Close();
  50.                 Thread.Sleep(65000 - curTime.Millisecond);
  51.             }
  52.         }
  53.  
  54.         static long PingSite(Ping pingSender, PingOptions options, string address, int attempt = 0)
  55.         {
  56.             PingReply reply = pingSender.Send(address, timeout, buffer, options);
  57.             if (reply.Status == IPStatus.Success)
  58.             {
  59.                 return reply.RoundtripTime;
  60.             }
  61.             else if (attempt < 5)
  62.             {
  63.                 Thread.Sleep(100);
  64.                 return PingSite(pingSender, options, address, ++attempt);
  65.             }
  66.             else
  67.             {
  68.                 return 999;
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment