【问题标题】:How to get interface name and IP address from this text file in Python如何在 Python 中从此文本文件中获取接口名称和 IP 地址
【发布时间】:2021-12-19 03:25:58
【问题描述】:

我想知道如何从文本文件中打印出每个接口及其各自的 IP 地址。 这是下面的文本文件:

eth0 Link encap:Ethernet HWaddr b8:ac:6f:65:31:e5 inet addr:192.168.2.100 
Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:fe65:31e5/64 
Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2697529 
errors:0 dropped:0 overruns:0 frame:0 TX packets:2630541 errors:0 dropped:0 
overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2159382827 (2.0 GiB) TX
bytes:1389552776 (1.2 GiB) 
 
eth1 Link encap:Ethernet HWaddr b8:ac:6f:65:53:e5 inet addr:10.10.2.100 Bcast:10.10.2.255 
Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:ff65:31e5/64 Scope:Link UP BROADCAST RUNNING
MULTICAST MTU:1500 Metric:1 RX packets:2697529 errors:0 dropped:0 overruns:0 frame:0 TX 
packets:2630541 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX 
bytes:2159382827 (2.0 GiB) TX bytes:1389552776 (1.2 GiB)

我已经对此进行了几次尝试。呈现给我的方式是把它变成一个列表并从那里解析它。

这是我尝试过的:

#Initializing a list
txt_lst = []
txt_file = open('ipaddress.txt', 'r', encoding = "utf8")
txt_lines = txt_file.readlines()

#Checks the length of the txtfile and list
sz = len(txt_lines)

#Adds each line to a list, essentially converting the textfile to a list
for line in txt_lines:
    txt_lst.append(line)

#Splitting each line by comma to create sentences for this textfile 
new_txt_lst = [line.split(",") for line in txt_lst]

这就是我想要格式化输出的方式:

interface name  |   ip address
eth0            |192.168.2.100
eth1            |10.10.2.100

任何帮助将不胜感激。谢谢!!

【问题讨论】:

  • 你能告诉我在哪里可以得到这些信息吗?我一直在努力,但是使用这个文本文件很复杂。
  • 在空行上拆分文本 - \n\n(双新行) - 然后第一个单词将是 interface name。如果您在inet addr: 上拆分,那么第一个“单词”将是IP address
  • for line in txt_lines: txt_lst.append(line) 给出与txt_lst = txt_line.copy() 相同的结果-但似乎浪费时间-您应该直接使用txt_line 而不是txt_lst

标签: python string parsing extract


【解决方案1】:

不要分割成行,而是将全文作为单个字符串处理。

如果您在空行上拆分 - \n\n(双新行) - 那么您应该将每个设备都作为单独的文本。

每个设备中的第一个单词是interface - 所以它需要split(' ', 1)[0] 来获取这个单词。

IP 地址在inet addr: 之后,因此您可以使用它来拆分文本并获取 [1] 以使文本以 IP 地址开头 - 因此您可以再次使用 split(' ', 1) and [0]` 来获取此 IP。


最少的工作代码。

我发现空行中有一个空格,所以它需要\n \n 而不是\n\n

text = '''eth0 Link encap:Ethernet HWaddr b8:ac:6f:65:31:e5 inet addr:192.168.2.100 
Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:fe65:31e5/64 
Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2697529 
errors:0 dropped:0 overruns:0 frame:0 TX packets:2630541 errors:0 dropped:0 
overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2159382827 (2.0 GiB) TX
bytes:1389552776 (1.2 GiB) 
 
eth1 Link encap:Ethernet HWaddr b8:ac:6f:65:53:e5 inet addr:10.10.2.100 Bcast:10.10.2.255 
Mask:255.255.255.0 inet6 addr: fe80::baac:6fff:ff65:31e5/64 Scope:Link UP BROADCAST RUNNING
MULTICAST MTU:1500 Metric:1 RX packets:2697529 errors:0 dropped:0 overruns:0 frame:0 TX 
packets:2630541 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX 
bytes:2159382827 (2.0 GiB) TX bytes:1389552776 (1.2 GiB)'''

devices = text.split('\n \n')

for dev in devices:
    name = dev.split(' ', 1)[0]
    ip = dev.split('inet addr:')[1].split(' ', 1)[0]
    print(name, ip)

结果:

eth0 192.168.2.100
eth1 10.10.2.100

顺便说一句:

如果你想获取当前计算机的接口,那么你可以使用模块psutil

import psutil

interfaces = psutil.net_if_addrs()

for key, val in interfaces.items():
    print(key, val[0].address)

在我的 Linux Mint 20(基于 Ubuntu 20.04)上的结果

lo 127.0.0.1
enp3s0 192.168.1.28
wlp5s0 192.168.1.31
docker0 172.17.0.1

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 2019-08-19
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多