private static void GetIPv4(string indentedLine, NetworkAdapterInfo adapter) { var addressText = ParseOutputTagFromLine(indentedLine, "inet addr:") ?? ParseOutputTagFromLine(indentedLine, "inet "); if (addressText == null) { return; } if (IPAddress.TryParse(addressText, out var outValue)) { adapter.IPv4 = outValue; } }
/// <summary> /// Retrieves the network adapters. /// </summary> /// <returns>A list of network adapters.</returns> public List <NetworkAdapterInfo> RetrieveAdapters() { const string HWaddr = "HWaddr "; const string Ether = "ether "; var result = new List <NetworkAdapterInfo>(); var interfacesOutput = ProcessRunner.GetProcessOutputAsync("ifconfig").Result; var wlanOutput = ProcessRunner.GetProcessOutputAsync("iwconfig") .Result.Split('\n') .Where(x => x.Contains("no wireless extensions.") == false) .ToArray(); var outputLines = interfacesOutput.Split('\n').Where(x => string.IsNullOrWhiteSpace(x) == false).ToArray(); for (var i = 0; i < outputLines.Length; i++) { // grab the current line var line = outputLines[i]; // skip if the line is indented if (char.IsLetterOrDigit(line[0]) == false) { continue; } // Read the line as an adatper var adapter = new NetworkAdapterInfo { Name = line.Substring(0, line.IndexOf(' ')).TrimEnd(':') }; // Parse the MAC address in old version of ifconfig; it comes in the first line if (line.IndexOf(HWaddr) >= 0) { var startIndexHwd = line.IndexOf(HWaddr) + HWaddr.Length; adapter.MacAddress = line.Substring(startIndexHwd, 17).Trim(); } // Parse the info in lines other than the first for (var j = i + 1; j < outputLines.Length; j++) { // Get the contents of the indented line var indentedLine = outputLines[j]; // We have hit the next adapter info if (char.IsLetterOrDigit(indentedLine[0])) { i = j - 1; break; } // Parse the MAC address in new versions of ifconfig; it no longer comes in the first line if (indentedLine.IndexOf(Ether) >= 0 && string.IsNullOrWhiteSpace(adapter.MacAddress)) { var startIndexHwd = indentedLine.IndexOf(Ether) + Ether.Length; adapter.MacAddress = indentedLine.Substring(startIndexHwd, 17).Trim(); } // Parse the IPv4 Address { var addressText = ParseOutputTagFromLine(indentedLine, "inet addr:") ?? ParseOutputTagFromLine(indentedLine, "inet "); if (addressText != null) { if (IPAddress.TryParse(addressText, out var outValue)) { adapter.IPv4 = outValue; } } } // Parse the IPv6 Address { var addressText = ParseOutputTagFromLine(indentedLine, "inet6 addr:") ?? ParseOutputTagFromLine(indentedLine, "inet6 "); if (addressText != null) { if (IPAddress.TryParse(addressText, out var outValue)) { adapter.IPv6 = outValue; } } } // we have hit the end of the output in an indented line if (j >= outputLines.Length - 1) { i = outputLines.Length; } } // Retrieve the wireless LAN info var wlanInfo = wlanOutput.FirstOrDefault(x => x.StartsWith(adapter.Name)); if (wlanInfo != null) { adapter.IsWireless = true; var essidParts = wlanInfo.Split(new[] { EssidTag }, StringSplitOptions.RemoveEmptyEntries); if (essidParts.Length >= 2) { adapter.AccessPointName = essidParts[1].Replace("\"", string.Empty).Trim(); } } // Add the current adapter to the result result.Add(adapter); } return(result.OrderBy(x => x.Name).ToList()); }