Python:检查网络接口是否已启动 [英] Python: check whether a network interface is up
本文介绍了Python:检查网络接口是否已启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,有没有办法检测给定的网络接口是向上?
In Python, is there a way to detect whether a given network interface is up?
In我的脚本,用户指定一个网络接口,但我想确保接口已启动,并且在执行任何其他操作之前已经分配了IP地址。
In my script, the user specifies a network interface, but I would like to make sure that the interface is up and has been assigned an IP address, before doing anything else.
我在 Linux ,我 root 。
推荐答案
As由@Gabriel Samfira建议,我使用 netifaces
。当IP地址与给定接口关联时,以下函数返回True。
As suggested by @Gabriel Samfira, I used netifaces
. The following function returns True when an IP address is associated to a given interface.
def is_interface_up(interface):
addr = netifaces.ifaddresses(interface)
return netifaces.AF_INET in addr
文档是此处
这篇关于Python:检查网络接口是否已启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文