Paul
Paul

Reputation: 5974

parsing ifconfig into lists

I want to parse ifconfig so that I am only left with the vlans that have an IP address assigned. I want the following data from each vlan, the interface name, the IP address and the mac address (mac in a converted format). These should be stored in a list of lists. A list for each vlan.

So far I have just made one list with the IP and the interface name for each vlan. However I feel I am going off track and doing this in a bad way, any suggestions?

Desired output:

[['vlan1', '192.168.2.2', '0013.F200.0058'], ['vlan100', '192.168.110.2','0013.F200.0058'], ['vlan20', '192.168.30.2','0013.F200.0058']]

Current output:

 ['vlan1', '0013.F200.0058', '192.168.2.2', 'vlan100', '0013.F200.0058', '192.168.110.2', 'vlan20', '0013.F200.0058', '192.168.30.2']

I know I can turn this flat list into nested lists easily, but I feel it should be done in the step before this.

EG but I think this is bad:

i=0
new_list=[]
while i<len(data_list):
    new_list.append(data_list[i:i+3])
    i+=3

Code:

import re
a = """

vlan1     Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:39 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2708 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2188 (2.1 KiB)  TX bytes:383156 (374.1 KiB)

vlan100   Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.110.2  Bcast:192.168.110.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2683 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:100 (100.0 B)  TX bytes:375620 (366.8 KiB)

vlan2     Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vlan20    Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.30.2  Bcast:192.168.30.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17274 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16376 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:4389760 (4.1 MiB)  TX bytes:3112376 (2.9 MiB)
"""

    for paragraph in a.split('\n\n'):

    #mac = re.search(r'HWaddr\s+(\S+)', paragraph)
    #c.append(mac.group(1))
    if "vlan" in paragraph and "inet addr:" in paragraph:
        b= []
        b.append(paragraph.split())
        for i in b[0]:     
            if "addr:" in i:
                ip = re.search(r'addr:(\S+)', i)
                c.append(ip.group(1))
            if "vlan" in i:
                c.append(i)   
            if  re.search(r'\d\d:\d\d:(\S+)', i):
                it = iter(i.split(':'))
                i = ".".join(x+y for x,y in zip(it,it))
                c.append(i) 
     print c

Upvotes: 1

Views: 2960

Answers (1)

furas
furas

Reputation: 142919

import re
a = """

vlan1     Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:39 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2708 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2188 (2.1 KiB)  TX bytes:383156 (374.1 KiB)

vlan100   Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.110.2  Bcast:192.168.110.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2683 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:100 (100.0 B)  TX bytes:375620 (366.8 KiB)

vlan2     Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vlan20    Link encap:Ethernet  HWaddr 00:13:F2:00:00:58  
          inet addr:192.168.30.2  Bcast:192.168.30.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17274 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16376 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:4389760 (4.1 MiB)  TX bytes:3112376 (2.9 MiB)
"""

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

    ma = re.compile("(vlan\d+).*HWaddr ([^ ]+).*addr:([^ ]+)", re.MULTILINE|re.DOTALL)

    result = ma.match(paragraph)

    if result != None:
        vlan = result.group(1)
        mac = result.group(2)
        ip = result.group(3)

        m = mac.split(':')
        mac = m[0] + m[1] + "." + m[2] + m[3] + "." + m[4] + m[5]

        #print "vlan:", vlan
        #print "ip:",ip
        #print "mac:", mac

        c.append([vlan, ip, mac])

print c

result:

[['vlan1', '192.168.2.2', '0013.F200.0058'], ['vlan100', '192.168.110.2', '0013.F200.0058'], ['vlan20', '192.168.30.2', '0013.F200.0058']]

Upvotes: 7

Related Questions