我想知道如何從文本檔案中列印出每個介面及其各自的 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
任何幫助將不勝感激。謝謝!!
uj5u.com熱心網友回復:
不要拆分成行,而是將全文作為單個字串處理。
如果您在空行上拆分 - \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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/351160.html
上一篇:AttributeError:'NoneType'物件沒有屬性'text',我不明白如何修復它
下一篇:使用bash-c逐行決議
