-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices-est.py
executable file
·186 lines (156 loc) · 5.39 KB
/
devices-est.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python3
from subprocess import run, PIPE
from sys import argv
from os import geteuid
from re import match
from typing import List, Mapping
from time import sleep, time
from json import dump
bssRegex = r'^BSS\s([0-9a-f:]*)'
stationCountRegex = r'.*station count: (\d*)'
utilRegex = r'.*channel utilisation: (\d*)\/(\d*)'
signalRegex = r'.*signal: ([-]?\d*\.\d*) dBm'
ssidRegex = r'[ \t]SSID: (.*)'
primaryChannelRegex = r'.*primary channel: (\d*)'
TTL = 300
outfile = None
def classifySignalQual(signal):
if signal >= -50.00:
return "excellent"
elif signal >= -60.00:
return "good"
elif signal >= -67.00:
return "reliable"
elif signal >= -70.00:
return "not good"
else:
return "unreliable"
class LRU(object):
"""
LRU is the LRU
"""
def __init__(self):
self.data: List[APInfo] = []
self.lastSeen: int = 0
class APInfo(object):
"""
APInfo contain info about access point, signal strength
"""
def __init__(self):
self.bss = ""
self.ssids = [""]
self.signal = 0.0
self.associated_count = 0
self.utilization = 0.0
self.channel = 0
self.last_seen = 0
def __gt__(self, apinfo2):
return (self.signal > apinfo2.signal)
def __str__(self):
return "%s channel %3d - (%10s), contain %2d ssids, devices=%3d, utils=%2.3f [%s]" % (self.bss, self.channel, classifySignalQual(self.signal), len(self.ssids), self.associated_count, self.utilization, ",".join(self.ssids))
aggregatedAPInfoes: List[APInfo] = []
grps: Mapping[str, LRU] = {}
def scan(deviceName):
APInfoes: List[APInfo] = []
result = run(['iw', 'dev', deviceName, 'scan', 'flush'],
stdout=PIPE).stdout.decode()
for line in result.splitlines():
bssMatch = match(bssRegex, line)
stationCountMatch = match(stationCountRegex, line)
utilsMatch = match(utilRegex, line)
signalMatch = match(signalRegex, line)
ssidMatch = match(ssidRegex, line)
channelMatch = match(primaryChannelRegex, line)
if bssMatch != None:
APInfoes.append(APInfo())
APInfoes[-1].bss = bssMatch.group(1)
elif stationCountMatch != None:
APInfoes[-1].associated_count = int(stationCountMatch.group(1))
elif utilsMatch != None:
APInfoes[-1].utilization = 100.0 * (int(utilsMatch.group(1)) /
int(utilsMatch.group(2)))
elif signalMatch != None:
APInfoes[-1].signal = float(signalMatch.group(1))
elif ssidMatch != None:
APInfoes[-1].ssids = [ssidMatch.group(1)]
elif channelMatch != None:
APInfoes[-1].channel = int(channelMatch.group(1))
return APInfoes
def objectify(ins: List[APInfo]):
return list(
map(
lambda i: {
"bss": i.bss,
"ssids": i.ssids,
"signal": i.signal,
"associated_count": i.associated_count,
"utilization": i.utilization,
"channel": i.channel,
"last_seen": i.last_seen
},
ins
),
)
if geteuid() != 0:
print("Root permission is required.")
exit(1)
if len(argv) < 3:
print(
"Usage: devices-est.py [wireless interface] [scan interval] [TTL] [out filename]")
exit(1)
if len(argv) >= 4:
TTL = int(argv[3])
if len(argv) == 5:
outfile = argv[4]
while True:
rawInfoes = scan(argv[1])
currenTime = int(time())
run('clear')
for a in sorted(rawInfoes, key=lambda n: [n.bss], reverse=True):
key = "%s*-%d" % (a.bss[0:-2], a.channel)
if key in grps:
if grps[key].lastSeen != currenTime:
grps[key].data = [a]
else:
grps[key].data.append(a)
grps[key].lastSeen = currenTime
else:
grps[key] = LRU()
grps[key].lastSeen = currenTime
grps[key].data = [a]
aggregatedAPInfoes = []
for key in list(grps.keys()):
if currenTime - int(grps[key].lastSeen) > TTL:
print("Haven't seen %s for more than %d secs" % (key, TTL))
del grps[key]
continue
aggInfo = APInfo()
aggInfo.bss = grps[key].data[0].bss[0:-2]+"*"
aggInfo.associated_count = grps[key].data[0].associated_count
aggInfo.utilization = grps[key].data[0].utilization
aggInfo.channel = grps[key].data[0].channel
aggInfo.ssids = list(map(lambda ai: ai.ssids[0], grps[key].data))
aggInfo.signal = sum(
list(map(lambda ai: ai.signal, grps[key].data))) / len(grps[key].data)
aggInfo.last_seen = grps[key].lastSeen
aggregatedAPInfoes.append(aggInfo)
aggregatedAPInfoes.sort()
for ai in aggregatedAPInfoes:
print(ai)
totalDev = sum(map(lambda i: i.associated_count, aggregatedAPInfoes))
print("devices = %d" % totalDev)
if outfile != None:
with open(outfile, mode="a") as target:
if target.tell() == 0:
target.write("[")
dump(
{
"ts": currenTime,
"totalDevs": totalDev,
"aps": objectify(aggregatedAPInfoes),
},
target
)
target.write(",")
target.close()
sleep(int(argv[2]))