Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- internal class Lab2
- {
- static byte[] IPv4ToBytes(string address)
- {
- byte[] bytes = new byte[4];
- string temp = "";
- int i = 0;
- int j = 0;
- while(i < address.Length)
- {
- if (address[i] == '.')
- {
- bytes[j++] = Convert.ToByte(temp);
- temp = "";
- }
- else
- temp += address[i];
- i++;
- }
- bytes[j] = Convert.ToByte(temp);
- return bytes;
- }
- static string bytesToIPv4(byte[] bytes)
- {
- string ip = "";
- for (int i = 0; i < 3; i++)
- {
- ip += bytes[i].ToString();
- ip += '.';
- }
- ip += bytes[3].ToString();
- return ip;
- }
- static string[] calcIPDiapason(string ip, string mask)
- {
- string[] diapason = new string[2];
- byte[] ipBytes = IPv4ToBytes(ip);
- byte[] maskBytes = IPv4ToBytes(mask);
- for(int i = 0;i < 4;i++)
- ipBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
- diapason[0] = bytesToIPv4(ipBytes);
- for(int i = 0;i < 4;i++)
- if (maskBytes[i] != 255)
- ipBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
- diapason[1] = bytesToIPv4(ipBytes);
- return diapason;
- }
- static bool isInDiapason(IPAddress address, string[] diapason)
- {
- if (address.ToString().CompareTo(diapason[0]) > 0 && address.ToString().CompareTo(diapason[1]) < 0)
- return true;
- else
- return false;
- }
- static string[] getDiapasonFromInput()
- {
- Console.Write("IP: ");
- string inputIP = Console.ReadLine();
- Console.Write("Mask: ");
- string inputMask = Console.ReadLine();
- string[] diapason = null;
- try
- {
- diapason = calcIPDiapason(inputIP, inputMask);
- }
- catch (Exception)
- {
- diapason = null;
- }
- return diapason;
- }
- static string getFirstMac()
- {
- string mac = "";
- foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
- {
- if (nic.OperationalStatus == OperationalStatus.Up)
- {
- mac = nic.GetPhysicalAddress().ToString();
- break;
- }
- }
- return mac;
- }
- static bool checkConfig()
- {
- const string OS = "Microsoft Windows NT 10.0.22621.0";
- const string mac = "7A791948F62B";
- const string cpuName = "Intel64 Family 6 Model 142 Stepping 12, GenuineIntel";
- string currOS = System.Environment.OSVersion.VersionString;
- string currMac = getFirstMac();
- string currCPU = System.Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").ToString();
- return currOS.Equals(OS) && currMac.Equals(mac) && currCPU.Equals(cpuName);
- }
- static void scanLAN(string[] diapason)
- {
- Console.WriteLine($"From {diapason[0]} to {diapason[1]}\n=================================");
- string macAddress = "";
- string ipAddress = "";
- string itemName = "";
- foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
- {
- foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
- {
- if (ip.Address.AddressFamily == AddressFamily.InterNetwork && isInDiapason(ip.Address, diapason))
- {
- IPInterfaceProperties props = item.GetIPProperties();
- ipAddress = ip.Address.ToString();
- macAddress = item.GetPhysicalAddress().ToString();
- if (macAddress.Length == 0)
- macAddress = "Не распознан";
- itemName = item.Name;
- Console.WriteLine("MAC: " + macAddress + "\nIP: " + ipAddress + '\n' + itemName + "\nDNS:");
- for (int i = 0; i < props.DnsAddresses.Count; i++)
- {
- Console.WriteLine(props.DnsAddresses.ElementAt(i).ToString());
- }
- Console.WriteLine("Interface type: " + item.NetworkInterfaceType.ToString());
- Console.WriteLine($"Mask: {ip.IPv4Mask}");
- Console.WriteLine("========================");
- }
- }
- }
- }
- private static void Main(string[] args)
- {
- if (checkConfig())
- {
- string[] diapason = getDiapasonFromInput();
- if (diapason != null)
- scanLAN(diapason);
- else
- Console.WriteLine("An error occured while trying to convert the ip and the mask!");
- }
- else
- Console.WriteLine("Access denied!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement