Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from subprocess import Popen, PIPE
  5. import fcntl
  6.  
  7. instructions = '''
  8. Usage: python reset_usb.py help : Show this help
  9. sudo python reset_usb.py list : List all USB devices
  10. sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
  11. sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
  12. sudo python reset_usb.py listpci : List all PCI USB devices
  13. sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
  14. sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
  15. '''
  16.  
  17.  
  18. if len(sys.argv) < 2:
  19. print(instructions)
  20. sys.exit(0)
  21.  
  22. option = sys.argv[1].lower()
  23. if 'help' in option:
  24. print(instructions)
  25. sys.exit(0)
  26.  
  27.  
  28. def create_pci_list():
  29. pci_usb_list = list()
  30. try:
  31. lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
  32. pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
  33. for pci_device in pci_devices:
  34. device_dict = dict()
  35. categories = pci_device.split(os.linesep)
  36. for category in categories:
  37. key, value = category.split('\t')
  38. device_dict[key[:-1]] = value.strip()
  39. if 'USB' not in device_dict['Class']:
  40. continue
  41. for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
  42. slot = device_dict['Slot']
  43. if slot in dirs:
  44. device_dict['path'] = os.path.join(root, slot)
  45. break
  46. pci_usb_list.append(device_dict)
  47. except Exception as ex:
  48. print('Failed to list pci devices! Error: %s' % ex)
  49. sys.exit(-1)
  50. return pci_usb_list
  51.  
  52.  
  53. def create_usb_list():
  54. device_list = list()
  55. try:
  56. lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
  57. usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
  58. for device_categories in usb_devices:
  59. if not device_categories:
  60. continue
  61. categories = device_categories.split(os.linesep)
  62. device_stuff = categories[0].strip().split()
  63. bus = device_stuff[1]
  64. device = device_stuff[3][:-1]
  65. device_dict = {'bus': bus, 'device': device}
  66. device_info = ' '.join(device_stuff[6:])
  67. device_dict['description'] = device_info
  68. for category in categories:
  69. if not category:
  70. continue
  71. categoryinfo = category.strip().split()
  72. if categoryinfo[0] == 'iManufacturer':
  73. manufacturer_info = ' '.join(categoryinfo[2:])
  74. device_dict['manufacturer'] = manufacturer_info
  75. if categoryinfo[0] == 'iProduct':
  76. device_info = ' '.join(categoryinfo[2:])
  77. device_dict['device'] = device_info
  78. path = '/dev/bus/usb/%s/%s' % (bus, device)
  79. device_dict['path'] = path
  80.  
  81. device_list.append(device_dict)
  82. except Exception as ex:
  83. print('Failed to list usb devices! Error: %s' % ex)
  84. sys.exit(-1)
  85. return device_list
  86.  
  87.  
  88. if 'listpci' in option:
  89. pci_usb_list = create_pci_list()
  90. for device in pci_usb_list:
  91. print('path=%s' % device['path'])
  92. print(' manufacturer=%s' % device['SVendor'])
  93. print(' device=%s' % device['SDevice'])
  94. print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
  95. sys.exit(0)
  96.  
  97. if 'list' in option:
  98. usb_list = create_usb_list()
  99. for device in usb_list:
  100. print('path=%s' % device['path'])
  101. print(' description=%s' % device['description'])
  102. print(' manufacturer=%s' % device['manufacturer'])
  103. print(' device=%s' % device['device'])
  104. print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
  105. sys.exit(0)
  106.  
  107. if len(sys.argv) < 3:
  108. print(instructions)
  109. sys.exit(0)
  110.  
  111. option2 = sys.argv[2]
  112.  
  113. print('Resetting device: %s' % option2)
  114.  
  115.  
  116. # echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
  117. def reset_pci_usb_device(dev_path):
  118. folder, slot = os.path.split(dev_path)
  119. try:
  120. fp = open(os.path.join(folder, 'unbind'), 'wt')
  121. fp.write(slot)
  122. fp.close()
  123. fp = open(os.path.join(folder, 'bind'), 'wt')
  124. fp.write(slot)
  125. fp.close()
  126. print('Successfully reset %s' % dev_path)
  127. sys.exit(0)
  128. except Exception as ex:
  129. print('Failed to reset device! Error: %s' % ex)
  130. sys.exit(-1)
  131.  
  132.  
  133. if 'pathpci' in option:
  134. reset_pci_usb_device(option2)
  135.  
  136.  
  137. if 'searchpci' in option:
  138. pci_usb_list = create_pci_list()
  139. for device in pci_usb_list:
  140. text = '%s %s' % (device['SVendor'], device['SDevice'])
  141. if option2 in text:
  142. reset_pci_usb_device(device['path'])
  143. print('Failed to find device!')
  144. sys.exit(-1)
  145.  
  146.  
  147. def reset_usb_device(dev_path):
  148. USBDEVFS_RESET = 21780
  149. try:
  150. f = open(dev_path, 'w', os.O_WRONLY)
  151. fcntl.ioctl(f, USBDEVFS_RESET, 0)
  152. print('Successfully reset %s' % dev_path)
  153. sys.exit(0)
  154. except Exception as ex:
  155. print('Failed to reset device! Error: %s' % ex)
  156. sys.exit(-1)
  157.  
  158.  
  159. if 'path' in option:
  160. reset_usb_device(option2)
  161.  
  162.  
  163. if 'search' in option:
  164. usb_list = create_usb_list()
  165. for device in usb_list:
  166. text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
  167. if option2 in text:
  168. reset_usb_device(device['path'])
  169. print('Failed to find device!')
  170. sys.exit(-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement