-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanZone.py
62 lines (51 loc) · 1.18 KB
/
scanZone.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
import pprint
import iwlist
import json
import sys
import time
INTERFACE = 'wlan0'
FILENAME = 'scan.json'
FIELD_TO_IGNORE = [
'cellnumber',
'encryprtion',
'frequency',
'frequency_units',
'mode',
'essid',
'signal_total'
]
pp = pprint.PrettyPrinter(indent=4)
if __name__ == '__main__':
if (len(sys.argv) != 3):
print "Usage: " + sys.argv[0] + " ZoneName " + "SampleNumber"
exit()
zoneName = sys.argv[1]
try:
sampleNumber = int(sys.argv[2])
except:
print "Non valid sample number"
exit()
res = []
for i in range(sampleNumber):
print "Scanning " + str(i+1) + "/" + str(sampleNumber)
content = iwlist.scan(interface=INTERFACE)
cells = iwlist.parse(content)
for el in cells:
for field in FIELD_TO_IGNORE:
el.pop(field, None)
# Corrupted data with positive dBm
for sample in cells:
if int(sample['signal_level_dBm']) > 0:
print 'Adjusting corrupted dBm'
sample['signal_level_dBm'] = "-100"
obj = {}
obj['scan'] = cells
obj['zone'] = zoneName
if cells != []:
res.append(obj)
else:
print 'Failed scan'
time.sleep(2)
#pp.pprint(res)
with open(zoneName + '.json', 'w+') as outfile:
json.dump(res, outfile, indent=4, sort_keys=True)