public static void PrintInterfaceInformation(NetworkInterface iface, int index = -1) { if (index != -1) { Logging.WriteConsole($"\t{index}: \"{iface.Name}\" <Description: {iface.Description} [{iface.OperationalStatus}]>"); } else { Logging.WriteConsole($"\t\"{iface.Name}\" <Description: {iface.Description} [{iface.OperationalStatus}]>"); } var addresses = iface.GetIPProperties().UnicastAddresses; if (addresses.Count > 0) { Logging.WriteConsole($"\t\tAddresses:"); foreach (var addr in addresses.Reverse()) { Logging.WriteConsole($"\t\t\t{addr.Address}"); } } else { Logging.WriteConsole("\t\tInterface has no addresses"); } string MAC = NetworkUtilities.hexMAC(iface.GetPhysicalAddress()); if (MAC.Length > 0) { Logging.WriteConsole($"\t\tMAC: {MAC}"); } Logging.WriteConsole($"\t\tType: {iface.NetworkInterfaceType}"); Logging.WriteConsole($"\t\tID: {iface.Id}"); Logging.WriteConsole(""); }
public static NetworkInterface?ValidateNetworkInterface(string interfaceCmdlineString) { if (interfaceCmdlineString == null) { return(null); } NetworkInterface?matchedInterface = null; NetworkInterface[] validInterfaces = GetInterfaces(); string comparer = interfaceCmdlineString.ToLower(); if (int.TryParse(comparer, out int indexInt)) { indexInt -= 1; if (indexInt >= 0 && indexInt < validInterfaces.Length) { return(validInterfaces[indexInt]); } } int index = 1; foreach (NetworkInterface iface in validInterfaces) { if (iface.Name.ToLower() == comparer) { matchedInterface = iface; break; } if (iface.Id.ToLower() == comparer || iface.Id.ToLower() == "{" + comparer + "}") { matchedInterface = iface; break; } if (iface.Description.ToLower() == comparer) { matchedInterface = iface; break; } string hexmac = NetworkUtilities.hexMAC(iface.GetPhysicalAddress()).ToLower(); if (hexmac.Length > 0 && comparer.Contains(hexmac)) { matchedInterface = iface; break; } foreach (var addr in iface.GetIPProperties().UnicastAddresses) { if (addr.Address.ToString() == comparer) { matchedInterface = iface; break; } } index += 1; } return(matchedInterface); }