如何在Ubuntu 18.04中使用Python脚本查询网卡信息及状态
在现代网络环境中,获取和管理网卡信息对于系统管理员和网络工程师来说是一项至关重要的任务。Ubuntu 18.04作为一个广泛使用的Linux发行版,提供了丰富的工具和库来支持这一需求。本文将详细介绍如何在Ubuntu 18.04中使用Python脚本查询网卡信息及状态,帮助读者更好地理解和掌握这一技能。
一、准备工作
在开始编写脚本之前,我们需要确保系统中已经安装了必要的Python环境和库。以下是准备工作步骤:
安装Python
:
Ubuntu 18.04默认安装了Python 3,可以通过以下命令验证:
python3 –version
安装必要的库
:
我们将使用
psutil
和
netifaces
库来获取网卡信息。可以通过以下命令安装:
sudo apt update
sudo apt install python3-psutil python3-netifaces
二、编写Python脚本
接下来,我们将编写一个Python脚本来查询网卡信息及状态。脚本将包括以下功能:
获取所有网卡列表。
查询每个网卡的基本信息(如IP地址、子网掩码、MAC地址等)。
查询网卡的状态(如是否启用、数据传输速率等)。
以下是完整的脚本代码:
import psutil
import netifaces as ni
def get_network_interfaces():
interfaces = ni.interfaces()
return interfaces
def get_interface_info(interface):
info = {}
try:
addresses = ni.ifaddresses(interface)
info[‘ip’] = addresses[ni.AF_INET][0][‘addr’]
info[‘netmask’] = addresses[ni.AF_INET][0][‘netmask’]
info[‘mac’] = addresses[ni.AF_LINK][0][‘addr’]
except ValueError:
info[‘ip’] = ‘Not available’
info[‘netmask’] = ‘Not available’
info[‘mac’] = ‘Not available’
return info
def get_interface_status(interface):
status = {}
if_stats = psutil.net_if_stats()[interface]
status[‘is_up’] = if_stats.isup
status[‘speed’] = if_stats.speed
return status
def main():
interfaces = get_network_interfaces()
for interface in interfaces:
print(f”Interface: {interface}”)
info = get_interface_info(interface)
status = get_interface_status(interface)
print(f” IP Address: {info[‘ip’]}”)
print(f” Netmask: {info[‘netmask’]}”)
print(f” MAC Address: {info[‘mac’]}”)
print(f” Is Up: {‘Yes’ if status[‘is_up’] else ‘No’}”)
print(f” Speed: {status[‘speed’]} Mbps”)
print()
if __name__ == “__main__”:
main()
三、脚本解析
导入库
:
import psutil
import netifaces as ni
psutil
用于获取系统信息,
netifaces
用于获取网络接口信息。
获取网卡列表
:
def get_network_interfaces():
interfaces = ni.interfaces()
return interfaces
ni.interfaces()
返回系统中所有网卡的列表。
获取网卡基本信息
:
def get_interface_info(interface):
info = {}
try:
addresses = ni.ifaddresses(interface)
info[‘ip’] = addresses[ni.AF_INET][0][‘addr’]
info[‘netmask’] = addresses[ni.AF_INET][0][‘netmask’]
info[‘mac’] = addresses[ni.AF_LINK][0][‘addr’]
except ValueError:
info[‘ip’] = ‘Not available’
info[‘netmask’] = ‘Not available’
info[‘mac’] = ‘Not available’
return info
通过
ni.ifaddresses(interface)
获取指定网卡的所有地址信息,包括IP地址、子网掩码和MAC地址。
获取网卡状态
:
def get_interface_status(interface):
status = {}
if_stats = psutil.net_if_stats()[interface]
status[‘is_up’] = if_stats.isup
status[‘speed’] = if_stats.speed
return status
psutil.net_if_stats()[interface]
返回指定网卡的统计信息,包括是否启用和数据传输速率。
主函数
:
def main():
interfaces = get_network_interfaces()
for interface in interfaces:
print(f”Interface: {interface}”)
info = get_interface_info(interface)
status = get_interface_status(interface)
print(f” IP Address: {info[‘ip’]}”)
print(f” Netmask: {info[‘netmask’]}”)
print(f” MAC Address: {info[‘mac’]}”)
print(f” Is Up: {‘Yes’ if status[‘is_up’] else ‘No’}”)
print(f” Speed: {status[‘speed’]} Mbps”)
print()
遍历所有网卡,打印每个网卡的基本信息和状态。
四、运行脚本
将上述代码保存为
network_info.py
,然后在终端中运行:
python3 network_info.py
输出结果将展示系统中每个网卡的基本信息和状态,如下所示:
Interface: eth0
IP Address: 192.168.1.100
Netmask: 255.255.255.0
MAC Address: 00:1A:2B:3C:4D:5E
Is Up: Yes
Speed: 1000 Mbps
Interface: wlan0
IP Address: 192.168.1.101
Netmask: 255.255.255.0
MAC Address: 00:1A:2B:3C:4D:5F
Is Up: Yes
Speed: 300 Mbps
五、总结
通过本文,我们学习了如何在Ubuntu 18.04中使用Python脚本查询网卡信息及状态。利用
psutil
和
netifaces
库,我们可以轻松获取网卡的基本信息和状态,这对于系统管理和网络故障排查具有重要意义。希望本文能对读者有所帮助,并在实际工作中发挥重要作用。
如果你有任何问题或需要进一步的帮助,请随时留言讨论。让我们一起探索更多有趣的Python应用场景!