Python 网络接口扫描
Python Network Interface Scan
我需要创建一个 Python 脚本来查询网络接口,returns 我需要主机名、IP 地址和 mac 地址。
#!/usr/bin/env python3
import netifaces
x = netifaces.interfaces()
i = x[0]
for i in x:
if i != 'lo':
print(i)
face = netifaces.ifaddresses(i)
print(face)
i += i
else:
continue
这是我正在使用的程序的一个版本。这似乎获取了我需要的所有数据,但我无法将其打印干净或正确!我正在寻找类似的东西:
"Nic: wlan0, ipaddr: 10.0.0.1, mac: 4651168584541"
我是编程的新手,也是 python 的新手,所以请多多帮助!
请检查链接:
@camflan 在
Getting MAC Address
How to get the physical interface IP address from an interface
import netifaces
x = netifaces.interfaces()
for i in x:
if i != 'lo':
print(i)
print("mac:" + netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr'] + " ipaddr:" + netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr'])
i += i
else:
continue
所以一开始这有点棘手,但这种格式应该允许您从 netifaces 的结果中获取数据。
import netifaces
x = netifaces.interfaces()
for i in x:
if i != 'lo':
print('\nInterface: ' + i)
mac = netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr']
print('Mac addr: ' + mac)
try:
ip = netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']
print('IP addr: {0} '.format(ip))
except KeyError:
print('NO IP')
continue
输出如下所示:
Interface: eth0
Mac add: eo:ie:9:38:ri
No IP
Interface: wlan0
Mac addr: 34:po:iu:66
IP addr: 10.0.0.1
我需要创建一个 Python 脚本来查询网络接口,returns 我需要主机名、IP 地址和 mac 地址。
#!/usr/bin/env python3
import netifaces
x = netifaces.interfaces()
i = x[0]
for i in x:
if i != 'lo':
print(i)
face = netifaces.ifaddresses(i)
print(face)
i += i
else:
continue
这是我正在使用的程序的一个版本。这似乎获取了我需要的所有数据,但我无法将其打印干净或正确!我正在寻找类似的东西: "Nic: wlan0, ipaddr: 10.0.0.1, mac: 4651168584541"
我是编程的新手,也是 python 的新手,所以请多多帮助!
请检查链接:
@camflan 在 Getting MAC Address
How to get the physical interface IP address from an interface
import netifaces x = netifaces.interfaces() for i in x: if i != 'lo': print(i) print("mac:" + netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr'] + " ipaddr:" + netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']) i += i else: continue
所以一开始这有点棘手,但这种格式应该允许您从 netifaces 的结果中获取数据。
import netifaces
x = netifaces.interfaces()
for i in x:
if i != 'lo':
print('\nInterface: ' + i)
mac = netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr']
print('Mac addr: ' + mac)
try:
ip = netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']
print('IP addr: {0} '.format(ip))
except KeyError:
print('NO IP')
continue
输出如下所示:
Interface: eth0
Mac add: eo:ie:9:38:ri
No IP
Interface: wlan0
Mac addr: 34:po:iu:66
IP addr: 10.0.0.1