import pyudev

# 创建Context对象
context = pyudev.Context()

# 创建Monitor对象,监控事件
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block',device_type='partition')
# 开始监控
monitor.start()

# 遍历检测到的USB设备
for device in iter(monitor.poll, None):
    if device.action == 'add':
        # 打印设备信息
        print("USB设备已添加:")
        print(f"设备路径: {device.device_path}")
        print(f"设备节点: {device.device_node}")
        print(f"设备ID: {device.sys_name}")
        print(f"设备序列号: {device.get('ID_SERIAL_SHORT')}")
        print(f"设备制造商: {device.get('ID_VENDOR_ID')}")
        print(f"设备产品: {device.get('ID_MODEL_ID')}")
        print(f"设备描述: {device.get('ID_MODEL_ENC')}")

    elif device.action == 'remove':
        # 打印设备移除信息
        print("USB设备已移除:")
        print(f"设备路径: {device.device_path}")
        print(f"设备节点: {device.device_node}")
        print(f"设备ID: {device.sys_name}")
        print(f"设备序列号: {device.get('ID_SERIAL_SHORT')}")
        print(f"设备制造商: {device.get('ID_VENDOR_ID')}")
        print(f"设备产品: {device.get('ID_MODEL_ID')}")
        print(f"设备描述: {device.get('ID_MODEL_ENC')}")
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐