.NET正则表达式解析ifconfig

时间:2015-06-10 20:06:59

标签: .net regex parsing ifconfig

我希望能够通过接口然后通过inet地址对ifconfig的输出进行分组。另外请假设接口块之间没有空格。

eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe12:3456/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1382 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1344 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:247209 TX bytes:131216
          Interrupt:9 Base address:0xc000
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope: Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1400 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1400 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:109731 TX bytes:109731
wlan0     Link encap:Ethernet  HWaddr CC:FA:00:A9:1D:7C
          inet addr:10.92.70.35  Bcast:10.92.71.255  Mask:255.255.252.0
          inet6 addr: 2001:4898:4070:101b:cefa:ff:fea9:1d7c/64 Scope: Global
          inet6 addr: fe80::cefa:ff:fea9:1d7c/64 Scope: Link
          inet6 addr: 2001:4898:4070:101b:1d4e:9b7f:f197:2e6f/64 Scope: Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:134563 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41967 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:75871586 TX bytes:9616476

会生成3个我可以参考的组,并确定它们是以eth还是wlan开头,然后从每个组中提取inet addr

到目前为止,我有@"inet addr:(?<IpV4Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"来获取inet地址,但我还没弄清楚如何将接口块分开。

1 个答案:

答案 0 :(得分:0)

以下是一种更好的方法:通过$ route确定默认界面并找到Destination = default的界面,然后执行$ ifconfig default_interface,其中default_interface是您确定的界面$ route。这样,只有一个接口块,您可以轻松解析inet addr