Search code examples
pythonstringparsingextract

How to get interface name and IP address from this text file in Python


I was wondering how to print out each interface and their respective IP address from a text file. Here is the text file below:

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

I've taken a crack at this a couple of times. The way that was presented to me was to turn it into a list and parse it from there.

Here is what I've tried:

#Initializing a list
txt_lst = []
txt_file = open('ipaddress.txt', 'r', encoding = "utf8")
txt_lines = txt_file.readlines()

#Checks the length of the txtfile and list
sz = len(txt_lines)

#Adds each line to a list, essentially converting the textfile to a list
for line in txt_lines:
    txt_lst.append(line)

#Splitting each line by comma to create sentences for this textfile 
new_txt_lst = [line.split(",") for line in txt_lst]

This is how I would like to format the output:

interface name  |   ip address
eth0            |192.168.2.100
eth1            |10.10.2.100

Any help will be appreciated. Thanks!!


Solution

  • Don't split into lines but work with full text as single string.

    If you split on empty line - \n\n (double new line) - then you should have every device as separated text.

    First word in every device is interface - so it needs split(' ', 1) and [0] to get this word.

    IP address is after inet addr: so you can use it to split text and get [1] to have text with IP address at the beginning - so you can use again split(' ', 1) and [0]` to get this IP.


    Minimal working code.

    I found that there is single space in empty line so it needs \n \n instead of \n\n.

    text = '''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) 
     
    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)'''
    
    devices = text.split('\n \n')
    
    for dev in devices:
        name = dev.split(' ', 1)[0]
        ip = dev.split('inet addr:')[1].split(' ', 1)[0]
        print(name, ip)
    

    Result:

    eth0 192.168.2.100
    eth1 10.10.2.100
    

    BTW:

    If you want to get interfaces for current computer then you can use module psutil

    import psutil
    
    interfaces = psutil.net_if_addrs()
    
    for key, val in interfaces.items():
        print(key, val[0].address)
    

    Result on my Linux Mint 20 (based on Ubuntu 20.04)

    lo 127.0.0.1
    enp3s0 192.168.1.28
    wlp5s0 192.168.1.31
    docker0 172.17.0.1