簡體   English   中英

如何在 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:之后inet addr:因此您可以使用它來拆分文本並獲取 [1] 以在開頭具有 IP 地址的文本 - 因此您可以再次使用split(' ', 1) and [0]` 來獲取此信息知識產權。


最少的工作代碼。

我發現空行中有一個空格,所以它需要\\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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM