Advertisement
Dimaland

Local devices

Feb 26th, 2024 (edited)
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.NetworkInformation;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         foreach (NetworkInterface interfac in NetworkInterface.GetAllNetworkInterfaces())
  9.         {
  10.             if (interfac.OperationalStatus == OperationalStatus.Up)
  11.             {
  12.                 Console.WriteLine("Имя интерфейса: " + interfac.Name);
  13.                 Console.Write("IPv4-адреса:");
  14.  
  15.                 // Получаем IPv4-адреса для текущего интерфейса
  16.                 foreach (UnicastIPAddressInformation ip in interfac.GetIPProperties().UnicastAddresses)
  17.                 {
  18.                     if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // Проверяем, что это IPv4-адрес
  19.                     {
  20.                         Console.WriteLine("  " + ip.Address.ToString());
  21.                         Console.WriteLine("Маска подсети: " + ip.IPv4Mask.ToString());
  22.                     }
  23.                 }
  24.  
  25.                 Console.WriteLine();
  26.             }
  27.         }
  28.  
  29.         Console.Write("\nВведите IP-адрес сети для сканирования: ");
  30.         string? ipAddress = Console.ReadLine();
  31.  
  32.         Console.Write("Введите маску подсети: ");
  33.         string? maskAddress = Console.ReadLine();
  34.         Console.WriteLine();
  35.  
  36.         IPAddress ipSearch;
  37.         IPAddress maskSearch;
  38.         if (IPAddress.TryParse(ipAddress, out ipSearch) && IPAddress.TryParse(maskAddress, out maskSearch))
  39.         {
  40.             ScanNetwork(ipSearch, maskSearch);
  41.         }
  42.         else
  43.         {
  44.             Console.WriteLine("Неверный формат IP-адреса или маски подсети.");
  45.         }
  46.  
  47.         Console.WriteLine("Сканирование завершено.");
  48.         Console.ReadLine();
  49.     }
  50.  
  51.     static void ScanNetwork(IPAddress ip, IPAddress mask)
  52.     {
  53.         IPAddress ipAddress = GetNetworkAddress(ip, mask);
  54.  
  55.         int loopCount = CalculateLoopCount(mask);
  56.  
  57.         switch (loopCount)
  58.         {
  59.             case 24:
  60.                 Mask_255_255_255_0(ipAddress);
  61.                 break;
  62.             case 16:
  63.                 Mask_255_255_0_0(ipAddress);
  64.                 break;
  65.             case 8:
  66.                 Mask_255_0_0_0(ipAddress);
  67.                 break;
  68.             default:
  69.                 Console.WriteLine("Неверный формат маски подсети.");
  70.                 break;
  71.         }
  72.  
  73.  
  74.     }
  75.  
  76.     static void Mask_255_255_255_0(IPAddress ipAddress)
  77.     {
  78.         Task[] tasks = new Task[254];
  79.  
  80.         for (int i = 1; i <= 254; i++)
  81.         {
  82.             string[] ipAddressParts = ipAddress.ToString().Split('.');
  83.             ipAddressParts[3] = i.ToString(); // Обновляем последний октет
  84.             IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
  85.  
  86.             tasks[i - 1] = Task.Factory.StartNew(() => {
  87.                 Ping ping = new Ping();
  88.                 PingReply reply = ping.Send(currentIpAddress);
  89.  
  90.                 if (reply.Status == IPStatus.Success)
  91.                 {
  92.                     string macAddress = GetMacAddress(currentIpAddress);
  93.  
  94.                     try
  95.                     {
  96.                         IPHostEntry hostEntry = Dns.GetHostEntry(currentIpAddress);
  97.                         Console.WriteLine($"IP-адрес: {currentIpAddress} доступен, Имя: {hostEntry.HostName}, MAC-адрес: {macAddress}.");
  98.                     }
  99.                     catch (Exception)
  100.                     {
  101.                         Console.WriteLine($"IP-адрес: {currentIpAddress} доступен, Имя: неизвестно, MAC-адрес: {macAddress}.");
  102.                     }
  103.                 }
  104.                 else
  105.                 {
  106.                     Console.WriteLine($"IP-адрес: {currentIpAddress} недоступен.");
  107.                 }
  108.             });
  109.         }
  110.  
  111.         Task.WaitAll(tasks);
  112.     }
  113.  
  114.     static void Mask_255_255_0_0(IPAddress ipAddress)
  115.     {
  116.  
  117.         for (int i = 1; i <= 254; i++)
  118.         {
  119.             string[] ipAddressParts = ipAddress.ToString().Split('.');
  120.             ipAddressParts[2] = i.ToString(); // Обновляем предпоследний октет
  121.             IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
  122.  
  123.             Mask_255_255_255_0(currentIpAddress);
  124.  
  125.         }
  126.  
  127.     }
  128.  
  129.     static void Mask_255_0_0_0(IPAddress ipAddress)
  130.     {
  131.         for (int i = 1; i <= 254; i++)
  132.         {
  133.             string[] ipAddressParts = ipAddress.ToString().Split('.');
  134.             ipAddressParts[1] = i.ToString(); // Обновляем второй октет
  135.             IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
  136.  
  137.             Mask_255_255_0_0(currentIpAddress);
  138.         }
  139.     }
  140.  
  141.     static string GetMacAddress(IPAddress ipAddress)
  142.     {
  143.         byte[] macAddr = new byte[6];
  144.         int macAddrLen = macAddr.Length;
  145.         string macAddress = "";
  146.         int ret = SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen);
  147.         if (ret == 0)
  148.         {
  149.             macAddress = BitConverter.ToString(macAddr, 0, macAddrLen);
  150.         }
  151.         return macAddress;
  152.     }
  153.  
  154.     [System.Runtime.InteropServices.DllImport("iphlpapi.dll", EntryPoint = "SendARP")]
  155.     internal extern static int SendARP(int destIP, int srcIP, byte[] pMacAddr, ref int phyAddrLen);
  156.  
  157.     static IPAddress GetNetworkAddress(IPAddress ip, IPAddress mask)
  158.     {
  159.         byte[] ipBytes = ip.GetAddressBytes();
  160.         byte[] maskBytes = mask.GetAddressBytes();
  161.  
  162.         byte[] resultBytes = new byte[ipBytes.Length];
  163.         for (int i = 0; i < ipBytes.Length; i++)
  164.         {
  165.             resultBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
  166.         }
  167.  
  168.        return new IPAddress(resultBytes);
  169.     }
  170.  
  171.     static int CalculateLoopCount(IPAddress mask)
  172.     {
  173.         byte[] maskBytes = mask.GetAddressBytes();
  174.         int loopCount = 0;
  175.         for (int i = 0; i < maskBytes.Length; i++)
  176.         {
  177.             for (int j = 7; j >= 0; j--)
  178.             {
  179.                 if (((maskBytes[i] >> j) & 1) == 0)
  180.                 {
  181.                     return loopCount;
  182.                 }
  183.                 loopCount++;
  184.             }
  185.         }
  186.         return loopCount;
  187.     }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement