简体   繁体   English

在Linux中的“以太网电缆插入”事件中得到通知

[英]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. 我已经在使用pyUdevlibUdev的 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.

相关问题 发现通过以太网电缆插入的设备? - Discover devices plugged in via ethernet cables? 在 Python 中确定计算机是否通过 WiFi 网络或以太网电缆连接到 Internet - Determine if a computer is connected to internet via WiFi network or ethernet cable in Python Python 套接字通过以太网电缆连接提供 [WinError 10057] - Python socket gives [WinError 10057] with Ethernet cable connection Linux:如何检测USB键盘是否插入和拔出 - Linux : How to detect if USB keyboard is plugged and unplugged Python:管理由其他线程通知的事件的线程 - Python: threads managing events notified by other threads 如何确定服务器断开连接(外壳电缆以太网断开) - How do you determine the server disconnection (case cut off cable ethernet) pythons“socket”库中的“recvfrom”无法在UDP实现中找到通过以太网电缆成功发送的消息 - “recvfrom” from pythons “socket” library cant find message successfully sent over ethernet cable in UDP implementation 如何在Linux下检测何时插入闪存驱动器? - How can I detect when a flash drive is plugged in under Linux? 当 usb 插入 Linux 机器时,如何运行带有导入的 python 脚本? - How to run a python script with imports when usb plugged into Linux machine? 电池是否实时插入(在Linux上),需要快速修复代码 - Is battery plugged or not in real time(on Linux), need quick fix in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM