如何监控USB设备插入?
How to monitor usb devices insertion?
我正在尝试在 USB 设备插入时对其进行监控。
几个测试脚本失败了,我很确定应该可以。
import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
for device in iter(monitor.poll, None):
if device.action == 'add':
print('{} connected'.format(device))
^^什么都不做。没有错误,没有输出。
我试试
import dbus
bus = dbus.SystemBus()
obj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
obj.GetDevices()
输出如下错误:
Traceback (most recent call last):
File "crap.py", line 4, in <module>
obj.GetDevices()
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: No such interface '(null)' on object at path /org/freedesktop/NetworkManager
dbus 在 Linux Mint 和 Pi3
上都阻止了我
如何在 python3 中监控 USB 设备?
pyudev
访问 linux udevadm
工具。使用此设备连接/分离事件由 udevadm monitor
监控
如果您使用 python 子进程调用调用 udevadm monitor
会发生什么情况?
from subprocess import call
call(["udevadm","monitor"])
如果你使用来自 pyudev 的 MonitorObserver
会发生什么?
您是否尝试过以 root 身份调用 python(脚本)?
以下两个变体在没有 root 的情况下为我工作,并作为带有 -i
选项的脚本调用:
from pyudev import Context, Monitor
context = Context()
monitor = Monitor.from_netlink(context)
device = monitor.poll(timeout=None)
if device:
print('{0.action}: {0}'.format(device))
-
from pyudev import Context, Monitor, MonitorObserver
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
def print_device_event(device):
print('background event {0.action}: {0.device_path}'.format(device))
observer = MonitorObserver(monitor, callback=print_device_event, name='monitor-observer')
observer.daemon
observer.start()
我正在尝试在 USB 设备插入时对其进行监控。 几个测试脚本失败了,我很确定应该可以。
import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
for device in iter(monitor.poll, None):
if device.action == 'add':
print('{} connected'.format(device))
^^什么都不做。没有错误,没有输出。
我试试
import dbus
bus = dbus.SystemBus()
obj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
obj.GetDevices()
输出如下错误:
Traceback (most recent call last):
File "crap.py", line 4, in <module>
obj.GetDevices()
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: No such interface '(null)' on object at path /org/freedesktop/NetworkManager
dbus 在 Linux Mint 和 Pi3
上都阻止了我如何在 python3 中监控 USB 设备?
pyudev
访问 linux udevadm
工具。使用此设备连接/分离事件由 udevadm monitor
如果您使用 python 子进程调用调用 udevadm monitor
会发生什么情况?
from subprocess import call
call(["udevadm","monitor"])
如果你使用来自 pyudev 的 MonitorObserver
会发生什么?
您是否尝试过以 root 身份调用 python(脚本)?
以下两个变体在没有 root 的情况下为我工作,并作为带有 -i
选项的脚本调用:
from pyudev import Context, Monitor
context = Context()
monitor = Monitor.from_netlink(context)
device = monitor.poll(timeout=None)
if device:
print('{0.action}: {0}'.format(device))
-
from pyudev import Context, Monitor, MonitorObserver
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
def print_device_event(device):
print('background event {0.action}: {0.device_path}'.format(device))
observer = MonitorObserver(monitor, callback=print_device_event, name='monitor-observer')
observer.daemon
observer.start()