[英]Getting Notified on “Ethernet cable plugged in” events in linux
I am writing a Python Application running on linux. 我正在编写在Linux上运行的Python应用程序。 I want to be able to register and be notified by the system if a network cable is plugged in/ out .
如果网络电缆已插入/拔出,我希望能够注册并得到系统通知 。
I am already using pyUdev (python bindings for libUdev) in order to get notified for USB plug in events. 我已经在使用pyUdev ( libUdev的 python绑定)来获取USB插入事件的通知。 However, because the network eth0 module (for example) always remains loaded (regardless of the cable being plugged in) , I dont get the information I require.
但是,由于网络eth0模块(例如)始终保持加载状态(无论是否插入电缆),我都无法获得所需的信息。 It only works for if up/down events.
它仅适用于上/下事件。
I have read a lot of posts on Windows WMI providing this functionality but none on linux. 我在Windows WMI上阅读了很多提供此功能的文章,但在Linux上却没有。 Am I looking in the right direction?
我在朝正确的方向看吗?
A python way of doing this (a python library) would be ideal as I want to integrate this with my program. 一种实现此功能的python方法(python库)非常理想,因为我想将其与程序集成。
There are many ways to get the "UP" status of a network interface via polling, ranging from simply parsing ifconfig output, to reading the status of /proc/net/... pseudo-files. 通过轮询获取网络接口的“ UP”状态的方法有很多,从简单地解析ifconfig输出到读取/ proc / net / ...伪文件的状态,不一而足。
However, for a way to be notified (ie not poll yourself) for such events, your best option on Ubuntu is to use the netplug package. 但是,为了获得一种通知 (即不自行查询)此类事件的方法,在Ubuntu上最好的选择是使用netplug软件包。 It's not installed by default, you need to: sudo apt-get install netplug
默认情况下未安装它,您需要:sudo apt-get install netplug
Some documentation is here: http://manpages.ubuntu.com/manpages/lucid/man8/netplugd.8.html 一些文档在这里: http : //manpages.ubuntu.com/manpages/lucid/man8/netplugd.8.html
To set it up you need to tweak /etc/netplug/netplugd.conf and /etc/netplug/netplug scripts. 要进行设置,您需要调整/etc/netplug/netplugd.conf和/ etc / netplug / netplug脚本。 For an example of those config files check out the section "Configuring netplug" at http://natisbad.org/dyn-net/index.html
有关这些配置文件的示例,请查看http://natisbad.org/dyn-net/index.html上的 “配置netplug”部分。
With /etc/netplug/netplug you can have your own python script called when the network goes down/up, so you can generate your kind of event for your application. 使用/ etc / netplug / netplug,您可以在网络中断/启动时调用自己的python脚本,因此可以为您的应用程序生成事件。
您是否看过netplugd命令,我不确定是否存在python包装器,但是解析输出应该不难
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.