Python:检查网络接口是否已启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17679887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:53:01  来源:igfitidea点击:

Python: check whether a network interface is up

pythonnetworkingipdhcpnetwork-interface

提问by Ricky Robinson

In Python, is there a way to detect whether a given network interfaceis up?

在 Python 中,有没有办法检测给定的网络接口是否已启动

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.

在我的脚本中,用户指定了一个网络接口,但在执行其他任何操作之前,我想确保该接口已启动并已分配 IP 地址。

I'm on Linuxand I am root.

我在Linux 上,我是root

采纳答案by Ricky Robinson

As suggested by @Gabriel Samfira, I used netifaces. The following function returns True when an IP address is associated to a given interface.

正如@Gabriel Samfira 所建议的,我使用了netifaces. 当 IP 地址与给定接口关联时,以下函数返回 True。

def is_interface_up(interface):
    addr = netifaces.ifaddresses(interface)
    return netifaces.AF_INET in addr

The documentation is here

文档在这里

回答by svinota

With pyroute2.IPRoute:

使用pyroute2.IPRoute:

from pyroute2 import IPRoute
ip = IPRoute()
state = ip.get_links(ip.link_lookup(ifname='em1'))[0].get_attr('IFLA_OPERSTATE')
ip.close()

With pyroute2.IPDB:

使用pyroute2.IPDB:

from pyroute2 import IPDB
ip = IPDB()
state = ip.interfaces.em1.operstate
ip.release()

回答by André Pires

You can see the content of the file in /sys/class/net/<interface>/operstate. If the content is not downthen the interface is up.

您可以在/sys/class/net/<interface>/operstate. 如果内容不是,down则界面已启动。

回答by user18197

The interface can be configured with an IP address and not be up so the accepted answer is wrong. You actually need to check /sys/class/net/<interface>/flags. If the content is in the variable flags, flags & 0x1is whether the interface is up or not.

接口可以配置一个 IP 地址,但不能启动,因此接受的答案是错误的。您实际上需要检查/sys/class/net/<interface>/flags. 如果内容在变量flags中,flags & 0x1就是接口是否up。

Depending on the application, the /sys/class/net/<interface>/operstatemight be what you really want, but technically the interface could be up and the operstatedown, e.g. when no cable is connected.

根据应用的不同,这/sys/class/net/<interface>/operstate可能是您真正想要的,但从技术上讲,接口可能是向上和operstate向下的,例如当没有连接电缆时。

All of this is Linux-specific of course.

当然,所有这些都是特定于 Linux 的。

回答by ofirule

Answer using psutil:

使用psutil回答:

import psutil
import socket

def check_interface(interface):
    interface_addrs = psutil.net_if_addrs().get(interface) or []
    return socket.AF_INET in [snicaddr.family for snicaddr in interface_addrs]

回答by myuce

If the question is about checking if the cable is conencted (FreeBSD);

如果问题是关于检查电缆是否已连接(FreeBSD);

[status for status in run.cmd(' /usr/local/bin/sudo ifconfig %s ' % interface).split("\t") if status.strip().startswith("status")][0].strip().endswith("active")

For this, no api support so far :( ...

为此,到目前为止还没有 api 支持:( ...