Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.NetworkInformation;
- using System.IO;
- using System.Threading;
- namespace Ping_Logger
- {
- class Program
- {
- static string data;
- static byte[] buffer;
- static int timeout;
- static void Main(string[] args)
- {
- Dictionary<string, string> addresses = new Dictionary<string, string>();
- addresses.Add("Router", "192.168.1.1");
- addresses.Add("Modem", "192.168.0.1");
- addresses.Add("ISP", "83.169.183.125");
- addresses.Add("GoogleLocal", "173.194.116.211");
- addresses.Add("GoogleDNS", "8.8.8.8");
- addresses.Add("PublicDNS", "4.2.2.2");
- Ping pingSender = new Ping();
- PingOptions options = new PingOptions();
- // Use the default Ttl value which is 128,
- // but change the fragmentation behavior.
- options.DontFragment = true;
- data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
- buffer = Encoding.ASCII.GetBytes(data);
- timeout = 120;
- // Create a buffer of 32 bytes of data to be transmitted.
- while (true)
- {
- StreamWriter sw = new StreamWriter(@"C:\Users\OMMITTED\Documents\Results\ping.txt", true);
- DateTime curTime = DateTime.Now;
- string newLine = string.Format("{0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString());
- foreach (var addy in addresses)
- {
- newLine += string.Format(",{0}", PingSite(pingSender, options, addy.Value));
- }
- sw.WriteLine(newLine);
- Console.WriteLine(newLine);
- sw.Close();
- Thread.Sleep(65000 - curTime.Millisecond);
- }
- }
- static long PingSite(Ping pingSender, PingOptions options, string address, int attempt = 0)
- {
- PingReply reply = pingSender.Send(address, timeout, buffer, options);
- if (reply.Status == IPStatus.Success)
- {
- return reply.RoundtripTime;
- }
- else if (attempt < 5)
- {
- Thread.Sleep(100);
- return PingSite(pingSender, options, address, ++attempt);
- }
- else
- {
- return 999;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment