Skip to content

Instantly share code, notes, and snippets.

@flaub
Created September 29, 2024 22:11
Show Gist options
  • Save flaub/732860656092bd4390e1a29987263a63 to your computer and use it in GitHub Desktop.
Save flaub/732860656092bd4390e1a29987263a63 to your computer and use it in GitHub Desktop.
USB Monitor
import time
from usbmonitor import USBMonitor
from usbmonitor.attributes import ID_MODEL, ID_MODEL_ID, ID_VENDOR_ID
def device_info_str(device_info):
model ="unknown"
if ID_MODEL in device_info:
model = device_info[ID_MODEL]
return f"{model} ({device_info[ID_MODEL_ID]} - {device_info[ID_VENDOR_ID]})"
def on_connect(device_id, device_info):
print(f"Connected: {device_id} -- {device_info_str(device_info=device_info)}")
def on_disconnect(device_id, device_info):
print(f"Disconnected: {device_id} -- {device_info_str(device_info=device_info)}")
def main():
monitor = USBMonitor()
for device_id, device_info in monitor.get_available_devices().items():
print(f"{device_id} -- {device_info_str(device_info)}")
monitor.start_monitoring(on_connect=on_connect, on_disconnect=on_disconnect)
while True:
time.sleep(10)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment