1

IP アドレスが割り当てられている VLAN のみが残るように、ifconfig を解析したいと考えています。各 vlan から次のデータ、インターフェイス名、IP アドレス、および MAC アドレス (変換された形式の mac) が必要です。これらは、リストのリストに格納する必要があります。各 VLAN のリスト。

これまでのところ、各 VLAN の IP とインターフェイス名を含む 1 つのリストを作成しました。しかし、私は軌道から外れており、これを悪い方法で行っていると感じています。何か提案はありますか?

望ましい出力:

[['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 に答える