监控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