1

我想解析 ifconfig 以便只剩下分配了 IP 地址的 vlan。我想要每个vlan的以下数据,接口名称,IP地址和mac地址(转换格式的mac)。这些应该存储在列表列表中。每个 vlan 的列表。

到目前为止,我刚刚列出了每个 vlan 的 IP 和接口名称。但是我觉得我正在偏离轨道并且以一种糟糕的方式这样做,有什么建议吗?

期望的输出:

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

电流输出:

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

我知道我可以轻松地将这个平面列表转换为嵌套列表,但我觉得应该在此之前的步骤中完成。

EG,但我认为这很糟糕:

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

代码:

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
4

1 回答 1

7
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

结果:

[['vlan1', '192.168.2.2', '0013.F200.0058'], ['vlan100', '192.168.110.2', '0013.F200.0058'], ['vlan20', '192.168.30.2', '0013.F200.0058']]
于 2013-06-14T11:26:47.217 回答