如何从 Python 中的此文本文件获取接口名称和 IP 地址
How to get interface name and IP address from this text file in Python
我想知道如何从文本文件中打印出每个接口及其各自的 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
- 所以它需要 split(' ', 1)
和 [0]
才能得到这个词。
IP 地址在 inet addr:
之后,因此您可以使用它来拆分文本并让 [1] 的文本以 IP 地址开头 - 这样您就可以再次使用 split(' ', 1) and
[0] ` 获取此 IP。
最少的工作代码。
我发现空行中有一个space所以它需要\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
我想知道如何从文本文件中打印出每个接口及其各自的 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
- 所以它需要 split(' ', 1)
和 [0]
才能得到这个词。
IP 地址在 inet addr:
之后,因此您可以使用它来拆分文本并让 [1] 的文本以 IP 地址开头 - 这样您就可以再次使用 split(' ', 1) and
[0] ` 获取此 IP。
最少的工作代码。
我发现空行中有一个space所以它需要\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