-
Notifications
You must be signed in to change notification settings - Fork 4
/
monitor.py
executable file
·88 lines (61 loc) · 2.16 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3
import sys
import signal
import subprocess
print("")
print("Using both an USB 3.0 and an USB 2.0 device (could be a thumb drive,")
print("an audio device or any other simple USB device), plug or unplug the")
print("device in the ports that you are interested for VM passthrough.")
print("")
print("Press Control + C when finished. The app will then print the device")
print("path of the USB ports. Also make sure that 'udevadm' is installed.")
print("")
print("Monitoring USB ports...")
###########################
# This gets the UDEV events
###########################
listout = []
def handle(sig, _):
if sig == signal.SIGINT:
print("")
signal.signal(signal.SIGINT, handle)
proc = subprocess.Popen(
["udevadm", "monitor", "-k", "-u", "-p", "-s", "usb"], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
if line.startswith(b'DEVPATH'):
listout.append(line)
proc.wait()
######################################
# This gets an unique list of DEVPATHs
######################################
# function to get unique values
def unique(input_list):
# leave only unique entries
return list(dict.fromkeys(input_list))
# function to remove the netries that are not useful for udev
def remove_unnecessary(input_list):
# copy to avoid modifying the input list
output_list = list(input_list)
# traverse for all elements
for element in output_list:
# remove long entries as they are not useful for udev
for potential_prefix in output_list:
if element != potential_prefix and element.startswith(potential_prefix):
output_list.remove(element)
return output_list
if __name__ == '__main__':
listout = [x.decode('utf-8').strip() for x in listout]
uniq = unique(listout)
filtered = remove_unnecessary(uniq)
print("\nFound these USB ports:")
print(*filtered, sep='\n')
print("")
orig_stdout = sys.stdout
with open("usb.portlist", "w+") as f:
sys.stdout = f
print(*filtered, sep='\n')
sys.stdout = orig_stdout
print("Results were saved to 'usb.portlist'.")