Skip to content

Instantly share code, notes, and snippets.

@maruno
Created September 16, 2018 12:29
Show Gist options
  • Save maruno/258e5d914876850f10833a719c839775 to your computer and use it in GitHub Desktop.
Save maruno/258e5d914876850f10833a719c839775 to your computer and use it in GitHub Desktop.
Read USB through ioreg plist
import asyncio
import plistlib
from typing import Iterable
def devices_from_bus(bus_entries: Iterable[dict]):
devices = []
for bus_entry in bus_entries:
if 'IORegistryEntryChildren' in bus_entry:
devices.extend(
devices_from_bus(bus_entry['IORegistryEntryChildren']))
else:
devices.append(bus_entry)
return devices
async def probe_ioreg():
process = await asyncio.create_subprocess_exec(
'ioreg', '-p', 'IOUSB', '-a', '-l',
stdout=asyncio.subprocess.PIPE)
ioreg_stdout: bytes = (await process.communicate())[0]
return plistlib.loads(ioreg_stdout, fmt=plistlib.FMT_XML)
def main():
loop = asyncio.get_event_loop()
ioreg = loop.run_until_complete(probe_ioreg())
devices = devices_from_bus(ioreg['IORegistryEntryChildren'])
for device in devices:
if not device['Built-In']:
print(f"Got device: {device['IORegistryEntryName']}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment