usb-watch/app/check_usb.py
2025-04-08 10:31:29 +02:00

41 lines
919 B
Python

import usb
from usb.core import USBError
### Some auxiliary functions ###
def _clean_str(s):
'''
Filter string to allow only alphanumeric chars and spaces
@param s: string
@return: string
'''
return ''.join([c for c in s if c.isalnum() or c in {' '}])
def _get_dev_string_info(device):
'''
Human readable device's info
@return: string
'''
try:
str_info = _clean_str(usb.util.get_string(device, 256, 2))
str_info += ' ' + _clean_str(usb.util.get_string(device, 256, 3))
return str_info
except USBError:
return str_info
def get_usb_devices():
'''
Get USB devices
@return: list of tuples (dev_idVendor, dev_idProduct, dev_name)
'''
return [(device.idVendor, device.idProduct, _get_dev_string_info(device))
for device in usb.core.find(find_all=True)
if device.idProduct > 2]