Добавить SubnetScanner/IsHostAlive.cs
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Net.NetworkInformation;
|
||||||
|
|
||||||
|
namespace IP_port_checker.SubnetScanner
|
||||||
|
{
|
||||||
|
public class IsHostAlive
|
||||||
|
{
|
||||||
|
private static bool HostAliveCheck(string ip)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (Ping ping = new Ping())
|
||||||
|
{
|
||||||
|
PingReply pingReply = ping.Send(ip, 1000);
|
||||||
|
return pingReply.Status == IPStatus.Success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void ParralelScan(string network)
|
||||||
|
{
|
||||||
|
var tasks = new List<Task>();
|
||||||
|
int alive = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 254; i++)
|
||||||
|
{
|
||||||
|
string ip = $"{network}.{i}";
|
||||||
|
tasks.Add(Task.Run(() =>
|
||||||
|
{
|
||||||
|
if (HostAliveCheck(ip))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Хост {ip} - в сети");
|
||||||
|
alive++;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Task.WaitAll(tasks.ToArray());
|
||||||
|
Console.WriteLine($"Устройств в сети: {alive}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user