Created
September 14, 2024 23:46
-
-
Save ikirigin/c38800d378d276a76623e7335f59789b to your computer and use it in GitHub Desktop.
MacOS python print USB devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import plistlib | |
# generated with claude. Buyer beware | |
def list_usb_devices(): | |
# Run system_profiler command to get USB device information | |
cmd = ['system_profiler', 'SPUSBDataType', '-xml'] | |
result = subprocess.run(cmd, capture_output=True, text=True) | |
# Parse the plist output | |
plist_data = plistlib.loads(result.stdout.encode('utf-8')) | |
# Extract USB devices information | |
usb_devices = plist_data[0]['_items'] | |
# Print information about each USB device | |
for device in usb_devices: | |
print_device_info(device) | |
def print_device_info(device, indent=''): | |
print(f"{indent}Name: {device.get('_name', 'Unknown')}") | |
print(f"{indent}Manufacturer: {device.get('manufacturer', 'Unknown')}") | |
if 'serial_num' in device: | |
print(f"{indent}Serial Number: {device['serial_num']}") | |
if '_items' in device: | |
for sub_device in device['_items']: | |
print(f"{indent}----") | |
print_device_info(sub_device, indent + ' ') | |
if __name__ == "__main__": | |
list_usb_devices() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment