Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- using System.Net.NetworkInformation;
- class Program
- {
- static void Main()
- {
- foreach (NetworkInterface interfac in NetworkInterface.GetAllNetworkInterfaces())
- {
- if (interfac.OperationalStatus == OperationalStatus.Up)
- {
- Console.WriteLine("Имя интерфейса: " + interfac.Name);
- Console.Write("IPv4-адреса:");
- // Получаем IPv4-адреса для текущего интерфейса
- foreach (UnicastIPAddressInformation ip in interfac.GetIPProperties().UnicastAddresses)
- {
- if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // Проверяем, что это IPv4-адрес
- {
- Console.WriteLine(" " + ip.Address.ToString());
- Console.WriteLine("Маска подсети: " + ip.IPv4Mask.ToString());
- }
- }
- Console.WriteLine();
- }
- }
- Console.Write("\nВведите IP-адрес сети для сканирования: ");
- string? ipAddress = Console.ReadLine();
- Console.Write("Введите маску подсети: ");
- string? maskAddress = Console.ReadLine();
- Console.WriteLine();
- IPAddress ipSearch;
- IPAddress maskSearch;
- if (IPAddress.TryParse(ipAddress, out ipSearch) && IPAddress.TryParse(maskAddress, out maskSearch))
- {
- ScanNetwork(ipSearch, maskSearch);
- }
- else
- {
- Console.WriteLine("Неверный формат IP-адреса или маски подсети.");
- }
- Console.WriteLine("Сканирование завершено.");
- Console.ReadLine();
- }
- static void ScanNetwork(IPAddress ip, IPAddress mask)
- {
- IPAddress ipAddress = GetNetworkAddress(ip, mask);
- int loopCount = CalculateLoopCount(mask);
- switch (loopCount)
- {
- case 24:
- Mask_255_255_255_0(ipAddress);
- break;
- case 16:
- Mask_255_255_0_0(ipAddress);
- break;
- case 8:
- Mask_255_0_0_0(ipAddress);
- break;
- default:
- Console.WriteLine("Неверный формат маски подсети.");
- break;
- }
- }
- static void Mask_255_255_255_0(IPAddress ipAddress)
- {
- Task[] tasks = new Task[254];
- for (int i = 1; i <= 254; i++)
- {
- string[] ipAddressParts = ipAddress.ToString().Split('.');
- ipAddressParts[3] = i.ToString(); // Обновляем последний октет
- IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
- tasks[i - 1] = Task.Factory.StartNew(() => {
- Ping ping = new Ping();
- PingReply reply = ping.Send(currentIpAddress);
- if (reply.Status == IPStatus.Success)
- {
- string macAddress = GetMacAddress(currentIpAddress);
- try
- {
- IPHostEntry hostEntry = Dns.GetHostEntry(currentIpAddress);
- Console.WriteLine($"IP-адрес: {currentIpAddress} доступен, Имя: {hostEntry.HostName}, MAC-адрес: {macAddress}.");
- }
- catch (Exception)
- {
- Console.WriteLine($"IP-адрес: {currentIpAddress} доступен, Имя: неизвестно, MAC-адрес: {macAddress}.");
- }
- }
- else
- {
- Console.WriteLine($"IP-адрес: {currentIpAddress} недоступен.");
- }
- });
- }
- Task.WaitAll(tasks);
- }
- static void Mask_255_255_0_0(IPAddress ipAddress)
- {
- for (int i = 1; i <= 254; i++)
- {
- string[] ipAddressParts = ipAddress.ToString().Split('.');
- ipAddressParts[2] = i.ToString(); // Обновляем предпоследний октет
- IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
- Mask_255_255_255_0(currentIpAddress);
- }
- }
- static void Mask_255_0_0_0(IPAddress ipAddress)
- {
- for (int i = 1; i <= 254; i++)
- {
- string[] ipAddressParts = ipAddress.ToString().Split('.');
- ipAddressParts[1] = i.ToString(); // Обновляем второй октет
- IPAddress currentIpAddress = IPAddress.Parse(string.Join('.', ipAddressParts));
- Mask_255_255_0_0(currentIpAddress);
- }
- }
- static string GetMacAddress(IPAddress ipAddress)
- {
- byte[] macAddr = new byte[6];
- int macAddrLen = macAddr.Length;
- string macAddress = "";
- int ret = SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen);
- if (ret == 0)
- {
- macAddress = BitConverter.ToString(macAddr, 0, macAddrLen);
- }
- return macAddress;
- }
- [System.Runtime.InteropServices.DllImport("iphlpapi.dll", EntryPoint = "SendARP")]
- internal extern static int SendARP(int destIP, int srcIP, byte[] pMacAddr, ref int phyAddrLen);
- static IPAddress GetNetworkAddress(IPAddress ip, IPAddress mask)
- {
- byte[] ipBytes = ip.GetAddressBytes();
- byte[] maskBytes = mask.GetAddressBytes();
- byte[] resultBytes = new byte[ipBytes.Length];
- for (int i = 0; i < ipBytes.Length; i++)
- {
- resultBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
- }
- return new IPAddress(resultBytes);
- }
- static int CalculateLoopCount(IPAddress mask)
- {
- byte[] maskBytes = mask.GetAddressBytes();
- int loopCount = 0;
- for (int i = 0; i < maskBytes.Length; i++)
- {
- for (int j = 7; j >= 0; j--)
- {
- if (((maskBytes[i] >> j) & 1) == 0)
- {
- return loopCount;
- }
- loopCount++;
- }
- }
- return loopCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement