python实现监控usb设备代码

文章介绍了一个Python脚本,使用WMI库来实时监控Windows电脑上插入的WinUSB设备,包括设备描述、ID、制造商等信息,并每秒自动检测。
摘要由CSDN通过智能技术生成

监控windows系统下的手机设备是否插入:

用于每秒监控windows电脑是否插入了手机设备(不支持硬盘),并显示手机设备id号,不同手机的id号是不同的
详细信息显示:对于检测到的每个WinUSB设备,脚本现在会打印出设备的描述、设备ID、即插即用设备ID,以及制造商(如果有的话)。
定期检查:脚本继续每1秒进行一次检查,这可以根据需要调整。
pip install WMI

import wmi
import time

def monitor_winusb_devices():
    c = wmi.WMI()

    print("Monitoring WinUSB devices... Press Ctrl+C to exit.")
    try:
        while True:
            for usb_device in c.Win32_PnPEntity():
                if usb_device.Description and "WinUSB" in usb_device.Description:
                    # 打印设备的详细信息
                    print(f"WinUSB device detected: {usb_device.Description}")
                    print(f"  Device ID: {usb_device.DeviceID}")
                    print(f"  PNP Device ID: {usb_device.PNPDeviceID}")
                    if usb_device.Manufacturer:
                        print(f"  Manufacturer: {usb_device.Manufacturer}")
                    print("------------------------------")
            time.sleep(1)  # 检查间隔设置为10秒
    except Exception as e:
        print(f"An error occurred: {e}")
    except KeyboardInterrupt:
        print("Stopped monitoring WinUSB devices.")

if __name__ == "__main__":
    monitor_winusb_devices()
------------------------------
Monitoring WinUSB devices... Press Ctrl+C to exit.
WinUSB device detected: WinUSB 设备
  Device ID: USB\VID_18D1&PID_4EE7\4CD899A2
  PNP Device ID: USB\VID_18D1&PID_4EE7\4CD899A2
  Manufacturer: WinUSB 设备
WinUSB device detected: WinUSB 设备
  Device ID: USB\VID_18D1&PID_4EE7\F2E635BB
  PNP Device ID: USB\VID_18D1&PID_4EE7\F2E635BB
  Manufacturer: WinUSB 设备

监控所有插入的usb设备

import wmi
import time

def monitor_usb_events():
    c = wmi.WMI()

    print("Monitoring USB devices... Press Ctrl+C to exit.")
    try:
        while True:
            for usb_device in c.Win32_PnPEntity():
                if usb_device.Description and "USB" in usb_device.Description:
                    print(f"USB device detected: {usb_device.Description}")
            time.sleep(10)  # 检查间隔设置为10秒
    except Exception as e:
        print(f"An error occurred: {e}")
    except KeyboardInterrupt:
        print("Stopped monitoring USB devices.")

if __name__ == "__main__":
    monitor_usb_events()
Monitoring USB devices... Press Ctrl+C to exit.
USB device detected: 符合 USB xHCI 的主机控制器
USB device detected: USB 输入设备
USB device detected: USB Composite Device
USB device detected: USB 输入设备
USB device detected: USB Composite Device
USB device detected: USB Composite Device
USB device detected: USB 输入设备
USB device detected: USB 输入设备
USB device detected: USB 根集线器(USB 3.0)
USB device detected: USB 输入设备
USB device detected: USB Audio Device
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值