gpt4 book ai didi

python - 通过Python解析 'ip addr'的输出

转载 作者:行者123 更新时间:2023-11-30 23:30:11 25 4
gpt4 key购买 nike

我需要一些帮助来解析 ip addr 的输出命令转储到文本文件,内容如下:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
1: lo inet 127.0.0.1/8 scope host lo
1: lo inet6 ::1/128 scope host \ valid_lft forever preferred_lft forever
2: em1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000\ link/ether b8:ca:3a:65:43:3c brd ff:ff:ff:ff:ff:ff
3: em2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000\ link/ether b8:ca:3a:65:43:3c brd ff:ff:ff:ff:ff:ff
4: em3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP qlen 1000\ link/ether b8:ca:3a:65:43:3e brd ff:ff:ff:ff:ff:ff
5: em4: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP qlen 1000\ link/ether b8:ca:3a:65:43:3e brd ff:ff:ff:ff:ff:ff
6: p1p1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether a0:36:9f:27:13:48 brd ff:ff:ff:ff:ff:ff
7: p1p2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether a0:36:9f:27:13:49 brd ff:ff:ff:ff:ff:ff
8: p1p3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether a0:36:9f:27:13:4a brd ff:ff:ff:ff:ff:ff
9: p1p4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether a0:36:9f:27:13:4b brd ff:ff:ff:ff:ff:ff
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP \ link/ether b8:ca:3a:65:43:3c brd ff:ff:ff:ff:ff:ff
10: bond0 inet6 fe80::baca:3aff:fe65:433c/64 scope link \ valid_lft forever preferred_lft forever
12: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP \ link/ether b8:ca:3a:65:43:3e brd ff:ff:ff:ff:ff:ff
12: bond1 inet6 fe80::baca:3aff:fe65:433e/64 scope link \ valid_lft forever preferred_lft forever

正如您所看到的,会有类似开头的行(^\d: $device),并且我不知道如何能够迭代该文件,并提取选择信息(ipv4 addr 、ipv6 地址(如果存在)、链路状态、硬件地址)(当此信息分布在多条线路上时)。

建议?

最佳答案

from itertools import groupby

interfaces = {}
with open('ip.txt') as f:
lines = [line for line in f if line.strip()]
# group by line number
for key, group in groupby(lines, lambda x: x.split()[0]):
interface = []
for thing in group:
# append lines without repeating part
interface += thing.split()[2:]
if interface:
interfaces[key] = interface

for key, interface in interfaces.items():
for x in ['inet', 'inet6', 'state', 'link/ether']:
if x in interface:
idx = interface.index(x)
print '%s %s=%s' % (key, x, interface[idx+1])


$ python ip.py
3: state=UP
3: link/ether=b8:ca:3a:65:43:3c
4: state=UP
4: link/ether=b8:ca:3a:65:43:3e
5: state=UP
5: link/ether=b8:ca:3a:65:43:3e
1: inet=127.0.0.1/8
1: inet6=::1/128
1: state=UNKNOWN
10: inet6=fe80::baca:3aff:fe65:433c/64
10: state=UP
10: link/ether=b8:ca:3a:65:43:3c
2: state=UP
2: link/ether=b8:ca:3a:65:43:3c
8: state=DOWN
8: link/ether=a0:36:9f:27:13:4a
9: state=DOWN
9: link/ether=a0:36:9f:27:13:4b
12: inet6=fe80::baca:3aff:fe65:433e/64
12: state=UP
12: link/ether=b8:ca:3a:65:43:3e
6: state=DOWN
6: link/ether=a0:36:9f:27:13:48
7: state=DOWN
7: link/ether=a0:36:9f:27:13:49

关于python - 通过Python解析 'ip addr'的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790590/

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