Advertisement
ErmineMD

CrazyPingTest

Jan 17th, 2017
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. using System;
  2. using System.Net.NetworkInformation;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5.  
  6. namespace ErmineMD.CrazyPingTest
  7. {
  8.     class Program
  9.     {
  10.         #region Trap application termination
  11.         // https://stackoverflow.com/questions/474679/capture-console-exit-c-sharp
  12.         [DllImport("Kernel32")]
  13.         private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
  14.  
  15.         private delegate bool EventHandler(CtrlType sig);
  16.         static EventHandler exitHandler;
  17.  
  18.         enum CtrlType
  19.         {
  20.             CTRL_C_EVENT = 0,
  21.             CTRL_BREAK_EVENT = 1,
  22.             CTRL_CLOSE_EVENT = 2,
  23.             CTRL_LOGOFF_EVENT = 5,
  24.             CTRL_SHUTDOWN_EVENT = 6
  25.         }
  26.  
  27.         static bool ExitHandler(CtrlType sig)
  28.         {
  29.             exiting = true;
  30.             needToExit.WaitOne();
  31.             needToExit.Dispose();
  32.             return true;
  33.         }
  34.         #endregion
  35.  
  36.         static Random rand = new Random();
  37.         static object randLocker = new object();
  38.         static SemaphoreSlim semaphore = new SemaphoreSlim(300, 300);
  39.         static bool exiting = false;
  40.         static CountdownEvent countdown = new CountdownEvent(1);
  41.         static AutoResetEvent needToExit = new AutoResetEvent(false);
  42.         static int j = 1;
  43.         static int index = 0;
  44.         // $a = 1..254 | % {([string]$_).padleft(3)} | get-random -count 254; for ($i = 0; $i -lt $a.length; $i += 16) { ($a[$i..($i+15)] -join ', ') + ',' }
  45.         static int[] array = {
  46.             205, 106, 145,  54, 227,  38, 240,  22,  84, 249, 197,  61, 251, 231, 250, 103,
  47.             112,  86,  47,  81, 136, 242,  64,  87, 222,  50,  19, 110, 125,   1, 244, 253,
  48.              25, 144,  93,  52,  90,   4,  99,  16, 243, 129, 164, 160, 188, 190,  69, 206,
  49.              44,  83,  79,   9, 209,  10,  23,  95, 175, 207,  89, 161,  39, 163, 141, 122,
  50.              28, 213,  24, 111,  32,  12, 174, 123,  56,  30, 155, 237, 181, 196, 116, 121,
  51.             166, 128,  27, 217,  62, 192,  34,   2, 194, 168, 224,  13,  33, 203,  88,  15,
  52.             186, 219,   3, 210, 176, 204, 130, 171,  58,   6,  94, 211, 232, 208, 150,  36,
  53.             179, 100,  51,  53, 254,  92, 235,  80, 228,  85,   5, 170,  35, 137,  49, 162,
  54.             115, 173, 191, 199, 183, 238, 225,  73,  42, 154,  77,  76, 133, 113, 119, 214,
  55.             195, 167, 229, 126, 153, 169,  46,  82, 124,  71,  63, 117, 236, 218, 220, 165,
  56.              21, 245,  68, 135, 198, 234, 247, 157,  96, 156, 193, 233, 127, 143, 159,  17,
  57.             230, 109,  70,  60,  65, 146,  40,  98, 182,  20, 101, 215,  41, 200,  59, 104,
  58.             216, 239,  14,  78,  43, 148,  67, 108,  29, 185,  57,  66,  11, 212,  75, 131,
  59.             184,  31,  91, 114, 134, 152, 105, 132, 189, 140,  37,  72,  18, 178,  26, 149,
  60.             151, 138, 202, 147, 172, 158,  55, 248, 241, 118, 142,   8, 107, 201,  97, 187,
  61.             177,  74, 226, 246, 139, 102, 120, 223,  45,   7, 252, 221,  48, 180 };
  62.         static int arrayLen = array.Length;
  63.  
  64.         public static void MyPing()
  65.         {
  66.             int i;
  67.             // Variant 1 - random order IP adresses
  68.             /*lock (randLocker)
  69.                 i = rand.Next(1, 254);*/
  70.  
  71.             // Variant 2 - normal order IP adresses
  72.             /*lock (randLocker)
  73.             {
  74.                 i = j;
  75.                 if (j == 50)
  76.                     j = 1;
  77.                 else
  78.                     ++j;
  79.             }*/
  80.  
  81.             // Variant 3 - fixed array of random IP adresses
  82.             lock (randLocker)
  83.             {
  84.                 i = array[index];
  85.                 if (index == arrayLen - 1)
  86.                     index = 0;
  87.                 else
  88.                     ++index;
  89.             }
  90.  
  91.  
  92.             string ipAddress = "192.168.1." + i;
  93.  
  94.             using (var ping = new Ping())
  95.             {
  96.                 try
  97.                 {
  98.                     Console.WriteLine(ipAddress.PadRight(20) + ">");
  99.                     var reply = ping.Send(ipAddress, 1000);
  100.                     Console.WriteLine(ipAddress.PadRight(20) + reply.Status.ToString());
  101.                 }
  102.                 catch (Exception ex)
  103.                 {
  104.                     Console.WriteLine("Error!!! " + ipAddress + "    " + ex.Message);
  105.                 }
  106.             }
  107.             semaphore.Release();
  108.             countdown.Signal();
  109.         }
  110.  
  111.         static void Main(string[] args)
  112.         {
  113.             exitHandler += new EventHandler(ExitHandler);
  114.             SetConsoleCtrlHandler(exitHandler, true);
  115.  
  116.             while (!exiting)
  117.             {
  118.                 semaphore.Wait();
  119.                 countdown.AddCount();
  120.                 //Thread.Sleep(10);
  121.                 Thread t = new Thread(MyPing);
  122.                 t.Start();
  123.             }
  124.  
  125.             countdown.Signal();
  126.             countdown.Wait();
  127.             countdown.Dispose();
  128.             semaphore.Dispose();
  129.  
  130.             // Not necessary
  131.             Console.WriteLine("--------------------------------------------------------- Exit ----");
  132.             Thread.Sleep(1000);
  133.  
  134.             needToExit.Set();
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement