-
Notifications
You must be signed in to change notification settings - Fork 1
/
kbdetector.py
executable file
·124 lines (98 loc) · 3.53 KB
/
kbdetector.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Lightweight script to detect and print out all keyboards connected (via USB including dongles / wireless receivers) to
the machine, without any external dependencies.
Supports Linux and MacOS using the following built-in tools:
Linux - lsusb
MacOS - system_profiler
"""
import argparse
import re
import logging
import subprocess
from sys import platform
logging.basicConfig(level=logging.INFO, format='%(message)s')
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--exclude", help="comma-separated list of keyboards to exclude")
parser.add_argument("-n", "--no-dongle", action="store_true", help="exclude keyboard dongles and/or receivers")
return parser.parse_args()
def linux_detect_kbs():
try:
device_pattern = b"Bus\\s+(?P<bus>\\d+)\\s+Device\\s+(?P<device>\\d+).+ID\\s(?P<id>\\w+:\\w+)\\s(?P<tag>.+)$"
device_re = re.compile(device_pattern, re.I)
devices = subprocess.check_output(
"lsusb -v 2>/dev/null | egrep '(^Bus|Keyboard)' | grep -B1 Keyboard",
shell=True
)
result = []
for device in devices.split(b'\n'):
if device:
match = device_re.match(device)
if match:
device_info = match.groupdict()
keeb = str(device_info['tag']).replace('b\'', '').replace('\'', '')
result.append(keeb)
return result
except BaseException as err:
return []
def mac_detect_kbs():
try:
# collect all connected device names as no further information about device type is available
devices = subprocess.check_output(
"""
system_profiler SPUSBDataType | egrep -B 2 -A 6 'Product ID' | sed 's/^--/#/'\
| egrep ':$' | sed -e 's/^ *//g' -e 's/ *:$//g'
""",
shell=True
)
result = []
for device in devices.split(b'\n'):
if device:
keeb = str(device).replace('b\'', '').replace('\'', '')
result.append(keeb)
# filter out obvious false positives
filtered = filter(
lambda k: "hub" not in str(k).lower() and "mouse" not in str(k).lower() and "usb" not in str(k).lower(),
result
)
return list(filtered)
except BaseException as err:
return []
def filter_dongles(kbs: list):
filtered = filter(
lambda k: "receiver" not in str(k).lower() and "dongle" not in str(k).lower(),
kbs
)
return list(filtered)
def print_keebs(kbs: list):
if kbs:
for kb in kbs:
k = u'\U00002328'
logging.info('%s %s', k, kb)
else:
logging.info("No keyboards detected...")
if __name__ == '__main__':
logging.info("""
_ _
| | _____ ___| |__ ___
| |/ / _ \/ _ \ '_ \/ __|
| < __/ __/ |_) \__ \
|_|\_\___|\___|_.__/|___/
""")
args = parse_args()
keebs = []
if "linux" in platform:
keebs = filter_dongles(linux_detect_kbs()) if args.no_dongle else linux_detect_kbs()
elif "darwin" in platform:
keebs = filter_dongles(mac_detect_kbs()) if args.no_dongle else mac_detect_kbs()
else:
logging.error("Unsupported platform: " + platform)
exit(1)
if args.exclude:
exclude = args.exclude.split(",")
filtered = [kb for kb in keebs if kb not in exclude]
print_keebs(filtered)
else:
print_keebs(keebs)