Skip to content

Instantly share code, notes, and snippets.

@shieldsd
Created February 3, 2016 14:46
Show Gist options
  • Save shieldsd/7994de2502d0f3ad94bf to your computer and use it in GitHub Desktop.
Save shieldsd/7994de2502d0f3ad94bf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import fcntl
import subprocess
import sys
# Equivalent of the _IO('U', 20) constant in the linux kernel.
USBDEVFS_RESET = ord('U') << (4*2) | 20
if __name__ == '__main__':
proc = subprocess.Popen(['lsusb'], stdout=subprocess.PIPE)
lines = proc.communicate()[0].split('\n')
path = None
for line in lines:
if sys.argv[1] in line:
parts = line.split()
bus = parts[1]
dev = parts[3][:3]
path = '/dev/bus/usb/%s/%s' % (bus, dev)
fd = os.open(path, os.O_WRONLY)
try:
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
finally:
os.close(fd)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment