Parsing windows 'ipconfig /all' output

最后都变了- 提交于 2020-02-05 02:31:07

问题


I am having a bit of trouble parsing the output of 'ipconfig /all' using a regex. Currently I am using RegexBuddy for testing, but I want to use the regex in C#.NET.

My output is:

Ethernet adapter Yes:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : MAC Bridge Miniport
   Physical Address. . . . . . . . . : 02-1F-29-00-85-C9
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a%24(Preferred)
   Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a7%24(Preferred)
   Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a8%24(Preferred)
   IPv4 Address. . . . . . . . . . . : 10.0.0.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   IPv4 Address. . . . . . . . . . . : 172.16.0.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 172.16.0.254
   DHCPv6 IAID . . . . . . . . . . . : 520228888
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-17-1C-CC-CF-00-1F-29-00-85-C9
   DNS Servers . . . . . . . . . . . : 192.162.100.15
                                       192.162.100.16
   NetBIOS over Tcpip. . . . . . . . : Enabled

The regex I have written so far is:

([ -~]+):.+(?:Description\s)(?:\.|\s)+:\s([ -~]+).+(?:Physical Address)(?:\.|\s)+:\s([ -~]+).+(?:DHCP Enabled)(?:\.|\s)+:\s([ -~]+).+(?:(?:Link-local IPv6 Address)(?:\.|\s)+:\s([ -~]+).+Preferred.+)+

The problem is that I want to capture all the useful fields as groups, to get them easily in C#, and for some reason - when I got to capture the multiple 'Link-local IPv6 Address' fields, it stopped working.

I'll appreciate any help, Thanks.

Edit: Another problem is that I receive the ipconfig data from a remote machine (there is an unmanaged program there which I don't have control of) - thus I can't use WMI or something like that to get the ipconfig info in another way.


回答1:


Why use regex? Your input is in simple key-value format. Use something along the lines of

foreach (var line in lines)
{
   var index  = line.IndexOf (':') ;
   if (index <= 0) continue ; // skip empty lines

   var key   = line.Substring (0,  index).TrimEnd (' ', '.') ;
   var value = line.Substring (index + 1).Replace ("(Preferred)", "").Trim () ;
}



回答2:


but I want to use the regex in C#.NET.

Why Regex? Believe me, you don't want to use regex. A wise man once said:

Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems.

Let me state your 2 problems right now:

  • Retrieve information about TCP/IP configuration using ipconfig
  • Parse the output from this tool using regex

Actually you could retrieve this information using WMI directly and thus solve your original problem and never think about using regexes again:

using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
using (var instances = mc.GetInstances())
{
    foreach (ManagementObject instance in instances)
    {
        if (!(bool)instance["ipEnabled"])
        {
            continue;
        }

        Console.WriteLine("{0}, {1}, {2}", instance["Caption"], instance["ServiceName"], instance["MACAddress"]);

        string[] ipAddresses = (string[])instance["IPAddress"];
        string[] subnets = (string[])instance["IPSubnet"];
        string[] gateways = (string[])instance["DefaultIPGateway"];
        string domains = (string)instance["DNSDomain"];
        string description = (string)instance["Description"];
        bool dhcp = (bool)instance["DHCPEnabled"];
        string[] dnses = (string[])instance["DNSServerSearchOrder"];
    }
}

In addition to that you could create strongly typed wrapper for those WMI classes using the Mgmtclassgen.exe utility making your code even safer and you will be able tpo get rid of the magic strings.




回答3:


Surely you can get all this information by using NetworkInterface.GetAllNetworkInterfaces()



来源:https://stackoverflow.com/questions/15184495/parsing-windows-ipconfig-all-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!