.NET regex to parse ifconfig

205 views Asked by At

I'd like to be able to group the output of ifconfig by interface and then by inet address. Also please assume that there is no whitespace in between the interface blocks.

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

Would produce 3 groups that I could reference and determine if they begin with eth or wlan and then pull the inet addr out of each of them

So far I have @"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]?))" to get the inet address but I haven't figured out how to get the interface blocks separated.

1

There are 1 answers

0
Reuben Tanner On BEST ANSWER

Here's a better way to do it: determine the default interface via $ route and finding the interface with Destination = default then do $ ifconfig default_interface where default_interface is the interface you determined from $ route. That way there's only one interface block and you can easily parse the inet addr