-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScannerUtils.py
183 lines (147 loc) · 5.49 KB
/
ScannerUtils.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
##########################################################################
# Author: Griffin Della Grotte ([email protected])
#
# This module interacts with the WiFi adapter at the lowest level,
# running the actual scan on the adapter and parsing its output into
# a usable format
##########################################################################
from subprocess import check_output, call
import os
import copy
import time
import threading
class APScanner(threading.Thread):
def __init__(self, lp=False, trigFunc=None):
threading.Thread.__init__(self)
self.foundAPs = []
self.mapAPs = []
self.loopPrint = lp
self.trigFuncs = [trigFunc]
self.__populateMap()
self.kill = False
def run(self):
self.__mainloop()
############################
# Map stuff
############################
def __populateMap(self):
# Populate the map given some data that computing servies provided
# (no longer used, not accurate enough)
import csv
with open('compsvcmap.csv', 'rt') as f:
reader = csv.reader(f)
csvlist = list(reader)
for line in csvlist:
if line[1] == "g-HT":
self.mapAPs.append(MapAP(line[2], line[0]))
def __getLocFromBSSID(self, bssid):
# Return the location from the seen BSSID
for i in range(len(self.mapAPs)):
ap = self.mapAPs[i]
if ap.bssid.lower() == bssid.lower():
return self.mapAPs[i].loc
return "Unknown AP"
#############################
# FoundAP stuff
#############################
def __rssiInt(self, rssiStr):
return int(float(rssiStr.split()[0]))
def __updateBSSIDWithRSSI(self, bssid, rssi, apList):
for ap in apList:
if ap.bssid == bssid:
ap.updateRSSI(rssi)
def __getAPFromBSSID(self, bssid, apList):
for ap in apList:
if bssid == ap.bssid:
return ap
def __sortAPList(self):
self.foundAPs.sort(reverse=True, key=lambda ap: ap.rssi)
# Sort by RSSI
self.foundAPs.sort(key=lambda ap: ap.age)
# Then by age
def __repr__(self):
print ("AGE\tSSID\t\tBSSID\t\t\tLOCATION\t\tRSSI\tFREQ")
for ap in self.foundAPs:
if ap.rssi < -70: continue
print (str(ap.age) + "\t" + ap.ssid + ("\t" if len(ap.ssid) >= 8 else "\t\t") + \
ap.bssid + "\t" + self.__getLocFromBSSID(ap.bssid) + \
"\t\t" + str(ap.rssi) + "\t" + ap.freq)
def getResultsList(self):
# Output for testing the RSSI packets
resl = []
for ap in self.foundAPs:
if ap.rssi < -60: continue
resl.append(
{"AGE":str(ap.age),
"SSID":ap.ssid,
"BSSID":ap.bssid,
"LOCATION":self.__getLocFromBSSID(ap.bssid),
"RSSI":str(ap.rssi),
"FREQ":str(ap.freq)}
)
return resl
############################
# Main loop
############################
def __mainloop(self):
devnull = open(os.devnull, 'w')
while not self.kill:
# This command actually gets the information out of the hardware
# Note: scan.awk is a gawk script I found online and then modified
out = check_output(
"echo 'password' | sudo -kS iw dev wlan0 scan | gawk -f scan.awk",
shell=True, stderr=devnull)
if (self.loopPrint): call(["clear"])
out=out.decode()
result=out.splitlines()
header=result.pop(0)
seenAPs = []
for line in result:
if len(line.strip()) < 1: continue
items = line.split()
seenAPs.append(FoundAP(items[0], items[1],
self.__rssiInt(items[3]), freq=items[2]))
if len(seenAPs) >= 1:
for ap in seenAPs:
if ap in self.foundAPs:
self.__updateBSSIDWithRSSI(ap.bssid, ap.rssi, self.foundAPs)
else:
self.foundAPs.append(ap)
#for ap in self.foundAPs:
# if ap not in seenAPs:
# ap.ageTick()
self.__sortAPList()
if (self.trigFuncs != None):
for f in self.trigFuncs:
f(self.getResultsList())
else:
# Error in scan
pass
if (self.loopPrint): self.__repr__()
time.sleep(0.01)
class FoundAP(object):
# Data class for organizing a found AP's information
def __init__(self, bssid, ssid, rssi, freq="None"):
self.bssid = bssid
self.rssi = rssi
self.ssid = ssid
self.freq = freq
self.age = 0
self.birth = time.time()
def updateRSSI(self, rssi):
self.rssi = rssi
self.age = 0
self.birth = time.time()
def ageTick(self):
self.age += 1
def getAgeMS(self):
return int(time.time()-birth)
def __eq__(self, other):
return (isinstance(other,type(self)) and
self.bssid == other.bssid)
def __repr__(self):
return "[ " + self.bssid + ":" + str(self.rssi) + " ]"
class MapAP(object):
def __init__(self, bssid, loc):
self.bssid = bssid
self.loc = loc