vile
vile

Reputation: 1

Trouble implementing the CIDR notation mask into my ip address output

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

Upvotes: 0

Views: 111

Answers (1)

VPfB
VPfB

Reputation: 17332

I'm posting a complete working program to show the advantage of JSON output:

import json
import subprocess

with subprocess.Popen("/usr/sbin/ip --json address".split(), stdout=subprocess.PIPE) as proc:
    ip = json.load(proc.stdout)
for iface in ip:
    name = iface['ifname']
    for info in iface['addr_info']:
        print(f"{name} | {info['local']}/{info['prefixlen']}")

Upvotes: 0

Related Questions