-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiwlist-parser.py
116 lines (88 loc) · 3.68 KB
/
iwlist-parser.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
#!/usr/bin/env python
import subprocess
import sys
cells = []
def generateData():
wlan = sys.argv[1] # setting wireless scanning interface
p = subprocess.Popen("iwlist " + wlan + " scanning > iwlist", shell=True)
(output, err) = p.communicate()
p.wait()
if err:
return False
else:
return True
def checkIfCell(data):
# receiving stripped data
if data.split(" ")[0] == "Cell":
return True
else:
return False
def getCellMac(data):
return data.split("Address:")[1].strip()
def usage():
print("""
Usage: python iwlist-parser.py [wlan interface]
Eg: python iwlist-parser.py wlan0
""")
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
sys.exit(1)
if generateData():
# parsing data
iwlist = open('iwlist')
data = iwlist.readlines()
parserCount = 0
cellIndex = -1
cellBssid = None
for x in data:
if parserCount is 0:
# skip first iteration
pass
else:
currentData = x.strip()
# check if it's the beginnning of a cell
if checkIfCell(currentData):
cellBssid = getCellMac(currentData)
cells.append({'bssid': cellBssid})
cellIndex += 1
else:
if cellBssid is not None:
iterData = currentData.split(":")
if len(iterData) == 2:
# formatting data
iData0 = iterData[0]
iData1 = iterData[1]
if iData0 == "Protocol":
iterData[1] = iData1.split(".11")[1]
if iData0 == "Frequency":
dat = iData1.split("GHz")
iterData[1] = dat[0].strip()
iChannel = dat[1].strip()
# retrieving channel parameter
cData = iChannel.split(" ")
cells[cellIndex].update({cData[0][1:].strip().lower(): cData[1][:-1]})
if iData0 == "Bit Rates":
iterData[1] = iData1.split("Mb/s")[0].strip()
if iData0 == "Extra":
wpaData = iData1.split("=")
if len(wpaData) == 2:
iterData[0] = wpaData[0].strip().lower()
iterData[1] = wpaData[1]
if wpaData[0] == "":
continue
cells[cellIndex].update(
{"_".join(iterData[0].strip().split(" ")).lower().replace("_(1)", ""): iterData[
1].strip()})
elif len(iterData) == 1:
# quality and signal level
qsData = iterData[0].split(" ")
if len(qsData) == 2:
quality = qsData[0].split("=")
level = qsData[1].split("=")
cells[cellIndex].update(
{"_".join(quality[0].split(" ")).lower(): quality[1].split("/")[0].strip(),
"_".join(level[0].split(" ")).lower(): level[1].split("dBm")[0].strip()})
parserCount += 1
print(cells)
print("\nNumber of APs detected: " + str(len(cells)))