0
votes

I'm working on my own personal task that helps me extract the ip addresses that start with "inet addr" from the "ip address" command in linux. I also wanted to extract the interfaces associated with each ip address.

For example something like this:

Interface name | IP address eth0 | 192.168.2.100/24 eth1 | 10.10.2.100/20

Here is the following output i'm working with:

eth0 Link encap:Ethernet HWaddr b8:ac:6f:65:31:e5 inet addr:192.168.2.100 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:fe65:31e5/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2697529 errors:0 dropped:0 overruns:0 frame:0 TX packets:2630541 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2159382827 (2.0 GiB) TX bytes:1389552776 (1.2 GiB) Interrupt:17

eth1 Link encap:Ethernet HWaddr b8:ac:6f:65:53:e5 inet addr:10.10.2.100 Bcast:10.10.2.255 Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:ff65:31e5/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2697529 errors:0 dropped:0 overruns:0 frame:0 TX packets:2630541 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2159382827 (2.0 GiB) TX bytes:1389552776 (1.2 GiB) Interrupt:17

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:2849 errors:0 dropped:0 overruns:0 frame:0 TX packets:2849 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2778290 (2.6 MiB) TX bytes:2778290 (2.6 MiB)

ppp0 Link encap:Point-to-Point Protocol inet addr:10.1.3.105 P-t-P:10.0.31.18 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1496 Metric:1 RX packets:102800 errors:0 dropped:0 overruns:0 frame:0 TX packets:63437 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:148532544 (141.6 MiB) TX bytes:4425518 (4.2 MiB)

vmnet1 Link encap:Ethernet HWaddr 00:50:56:c0:00:01 inet addr:192.168.47.1 Bcast:192.168.47.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:49 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

vmnet8 Link encap:Ethernet HWaddr 00:50:56:c0:00:08 inet addr:172.16.232.1 Bcast:172.16.232.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fec0:8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:49 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

vmnet9 Link encap:Ethernet HWaddr 00:50:56:c0:01:08 inet addr:172.16.233.1 Bcast:172.16.233.255 Mask:255.255.255.0 inet6 addr: fe80::250:76ff:fec0:8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:49 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

vmnet10 Link encap:Ethernet HWaddr 00:50:56:c0:02:08 inet addr:172.16.234.1 Bcast:172.16.234.255 Mask:255.255.255.0 inet6 addr: fe80::250:86ff:fec0:8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:49 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

Here is the following code I have done and worked with:

import re

with open('rawdata.txt', 'r') as file:
    data = file.read()



s = []
subnetM = re.findall(r'(?<=Mask:255.)(.*)', data)
position = 0
for i in subnetM:
    if i == "255.255.0":
        s.insert(position, "/24")
    if i == "255.255.255":
        s.insert(position, "/32")
    if i == "0.0.0":
        s.insert(position, "/8")
    position = position + 1

c = []
for paragraph in data.split('\n\n'):

    ma = re.compile("^(\S+).*?inet addr:(\S+)", re.MULTILINE | re.DOTALL)
    result = ma.match(paragraph)

    if result != None:
        result = ma.match(paragraph)

        interface = result.group(1)
        ip = result.group(2)

        c.append([interface, ip])

print("Interface name | IP address")

for (interface, ip) in c:
    print(interface, ' | ', ip)

Although this works fine, I can't seem to extract the subnet mask prefixes such as /24, /25, /26 with the ip addresses.

Here is the following output I get: Output

Do not parse the output for humans with regular expressions! Run the command as ip --json address to produce a machine readable JSON. The JSON is a list of nested dict with all the data nicely structured. Tip: install the jq utiliy and run ip --json address | jq . on the command line to see the structure pretty formatted (and in color). BTW, the jq alone can probably solve the task if you are willing to learn a new tool.VPfB
Yeah I have looked into that, but I am currently trying to work with regex as I am a beginner with it. Just trying to further develop my coding skills and suchvile
To select the right tool for a task is one of the skills to be learned. Do you know the insider joke about regex: "... Now they have two problems." ?VPfB