Skip to content

Instantly share code, notes, and snippets.

@opragel
Last active April 19, 2025 22:17
Show Gist options
  • Save opragel/c662e33c636123f74726d52136ebc0c3 to your computer and use it in GitHub Desktop.
Save opragel/c662e33c636123f74726d52136ebc0c3 to your computer and use it in GitHub Desktop.
ea_get_connected_smartcard_readers.py
#!/usr/bin/python3
"""Intends to read USB SmartCard readers and return
device names, product IDs, and vendor IDs"""
import json
import plistlib
import subprocess
def main():
DRIVER_INFO_PLIST_PATH = open('/usr/libexec/SmartCardServices/drivers/ifd-ccid.bundle/Contents/Info.plist', 'rb')
drive_info_plist = plistlib.load(DRIVER_INFO_PLIST_PATH)
product_ids = drive_info_plist['ifdProductID']
vendor_ids = drive_info_plist['ifdVendorID']
cmd = ['/usr/sbin/system_profiler', 'SPUSBDataType', '-json']
output = subprocess.check_output(cmd)
info = json.loads(output)
hardware_info = info['SPUSBDataType']
items = []
for dict in hardware_info:
if "_items" in dict:
for item in dict["_items"]:
if item['vendor_id'] in vendor_ids and item['product_id'] in product_ids:
items.append(f"{item['_name']},{item['vendor_id']},{item['product_id']}")
if not items:
items = "N/A"
print(f"<result>{items}</result>")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment