gpt4 book ai didi

python - 将 ifconfig 解析为列表

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:21 24 4
gpt4 key购买 nike

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

到目前为止,我已经为每个 vlan 制作了一个包含 IP 和接口(interface)名称的列表。但是我觉得我偏离了轨道并且以一种糟糕的方式这样做,有什么建议吗?

期望的输出:

[['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

最佳答案

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']]

关于python - 将 ifconfig 解析为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17106362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com