简体   繁体   English

如何在 Python 中从此文本文件中获取接口名称和 IP 地址

[英]How to get interface name and IP address from this text file in Python

I was wondering how to print out each interface and their respective IP address from a text file.我想知道如何从文本文件中打印出每个接口及其各自的 IP 地址。 Here is the text file below:这是下面的文本文件:

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)

I've taken a crack at this a couple of times.我已经尝试过几次了。 The way that was presented to me was to turn it into a list and parse it from there.呈现给我的方法是将它变成一个列表并从那里解析它。

Here is what I've tried:这是我尝试过的:

#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]

This is how I would like to format the output:这就是我想格式化输出的方式:

interface name  |   ip address
eth0            |192.168.2.100
eth1            |10.10.2.100

Any help will be appreciated.任何帮助将不胜感激。 Thanks!!谢谢!!

Don't split into lines but work with full text as single string.不要拆分成行,而是将全文作为单个字符串处理。

If you split on empty line - \\n\\n (double new line) - then you should have every device as separated text.如果您在空行上拆分 - \\n\\n (双换行) - 那么您应该将每个设备作为单独的文本。

First word in every device is interface - so it needs split(' ', 1) and [0] to get this word.每个设备中的第一个字是interface - 所以它需要split(' ', 1)[0]来得到这个字。

IP address is after inet addr: so you can use it to split text and get [1] to have text with IP address at the beginning - so you can use again split(' ', 1) and [0]` to get this IP. IP 地址在inet addr:之后inet addr:因此您可以使用它来拆分文本并获取 [1] 以在开头具有 IP 地址的文本 - 因此您可以再次使用split(' ', 1) and [0]` 来获取此信息知识产权。


Minimal working code.最少的工作代码。

I found that there is single space in empty line so it needs \\n \\n instead of \\n\\n .我发现空行中有一个空格,所以它需要\\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)

Result:结果:

eth0 192.168.2.100
eth1 10.10.2.100

BTW:顺便提一句:

If you want to get interfaces for current computer then you can use module psutil如果要获取当前计算机的接口,则可以使用模块psutil

import psutil

interfaces = psutil.net_if_addrs()

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

Result on my Linux Mint 20 (based on Ubuntu 20.04)在我的 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