Python: check whether a network interface is up

前端 未结 6 453
孤城傲影
孤城傲影 2020-12-03 15:52

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

In my script, the user specifies a network interface,

6条回答
  •  广开言路
    2020-12-03 16:38

    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//flags. If the content is in the variable flags, flags & 0x1 is whether the interface is up or not.

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

    All of this is Linux-specific of course.

提交回复
热议问题