Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- namespace SweePing
- {
- class Program
- {
- static void Main(String[] args)
- {
- var p = new Program();
- var _stopWatch = Stopwatch.StartNew();
- p.PingSweepAsync("10.250.0.", 1, 254).GetAwaiter().GetResult();
- _stopWatch.Stop();
- Console.WriteLine($"{p.HostsUp} devices found! Elapsed time: {_stopWatch.ElapsedMilliseconds}ms");
- if (Debugger.IsAttached)
- {
- Console.WriteLine("Press ENTER to EXIT . . .");
- Console.ReadLine();
- }
- }
- private Int32 _timeout = 100;
- static Object LockObj = new Object();
- public Int32 HostsUp { get; set; } = 0;
- public async Task PingSweepAsync(String baseIp, Int16 start, Int16 end)
- {
- HostsUp = 0;
- var tasks = new List<Task>();
- for (var i = start; i <= end; i++)
- {
- tasks.Add(PingAndUpdateAsync(baseIp + i.ToString()));
- }
- await Task.WhenAll(tasks);
- }
- private async Task PingAndUpdateAsync(String ip)
- {
- var ping = new Ping();
- var reply = await ping.SendPingAsync(ip, _timeout);
- if (reply.Status == IPStatus.Success)
- {
- lock (LockObj)
- {
- HostsUp++;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment