解析Windows'ipconfig/all'输出 [英] Parsing windows 'ipconfig /all' output

查看:101
本文介绍了解析Windows'ipconfig/all'输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用正则表达式解析"ipconfig/all"的输出时遇到了一些麻烦. 目前,我正在使用RegexBuddy进行测试,但是我想在C#.NET中使用正则表达式.

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.

我的输出是:

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.+)+

问题是我想将所有有用的字段按组捕获,以便在C#中轻松获取它们,并且由于某些原因-当我捕获多个链接本地IPv6地址"字段时,它停止工作.

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.

另一个问题是我从远程计算机接收到ipconfig数据(那里有一个不受管理的程序,我无法控制该程序)-因此,我无法使用WMI或类似方法以其他方式获取ipconfig信息.

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.

推荐答案

为什么使用正则表达式?您的输入采用简单的键值格式.沿用

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 () ;
}

这篇关于解析Windows'ipconfig/all'输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆