Created
February 3, 2016 14:46
-
-
Save shieldsd/7994de2502d0f3ad94bf to your computer and use it in GitHub Desktop.
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
#!/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