-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifitools.py
More file actions
181 lines (155 loc) · 5.94 KB
/
wifitools.py
File metadata and controls
181 lines (155 loc) · 5.94 KB
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
import time
import machine
import network
import binascii
#
# wlan - A class that contains a WLAN we are aware of.
#
class WLAN:
def __init__(self,ssid,bssid,channel,rssi,security,hidden):
self.ssid = ssid
self.bssid = bssid
self.channel = channel
self.rssi = rssi
self.security = security
self.hidden = hidden
self.count = 0
def getSSID( self, width=24 ):
ssid = self.ssid[:24]
while len(ssid) < 24:
ssid += ' '
return ssid
def getBSSID( self, width=24 ):
bssid = self.bssid[:24]
while len(bssid) < 24:
bssid += ' '
return bssid
def getSecurity( self ):
# 0 1 2 3 4 5 6
security = ["OPEN", "WEP-PSK", "WPA", "WEP-PSK/WPA", "WPA2", "WEP-PSK/WPA2", "WPA2/WPA", "WEP-PSK/WPA/WPA2"]
if self.security < len(security):
return security[self.security]
else:
return str(self.security)
def __str__(self):
if self.ssid == "":
msg = f"{self.getBSSID()}"
else:
msg = f"{self.getSSID()}\t{self.channel:>2}\t{self.rssi:>3}"
return msg
def __repr__( self ):
return f"WLAN(\"{self.ssid}\", {self.bssid}, {self.channel}, {self.rssi}, {self.security}, {self.hidden} )"
#
# WLANList - This class is used to create a list of the WLAN's we have seen during a scan.
#
# This class supports being itereated, printed, and supports random access and obtaining the length.
#
class WLANList:
def __init__( self ):
self.wlanlist = []
self.count = 0
def __iter__(self):
'''
iterator - Make it so that we can iterate over this class, just like an array
'''
self.index = -1
return self
def __next__(self):
'''
next - Obtain the next item in the list, assuming __iter__ was called first
'''
self.index += 1
if self.index < len(self.wlanlist):
return self.wlanlist[self.index]
else:
raise StopIteration
def __getitem__( self, index ):
'''
This function is used to access a specific item within the list, allowing this
class to be used like and array (i.e. it implments the required code for making
[] work.
'''
if index < len(self.wlanlist):
return self.wlanlist[index]
else:
raise StopIteration
def __len__( self ):
'''
__len__ - This function returns the number of items within the WLANList.
'''
return len(self.wlanlist)
def __repr__( self ):
data = f"WLANList( {self.count}, {self.wlanlist})"
return data
def addItem( self, lan ):
'''
addItem - This function adds an item into the WLANList, if and only if it is not a duplicate.
The definition of a duplicate is the BSSID matches.
'''
for item in self.wlanlist:
if item.ssid == lan.ssid:
item.rssi = lan.rssi
item.channel = lan.channel
return
elif item.bssid == lan.bssid:
item.rssi = lan.rssi
item.channel = lan.channel
return
lan.count = self.count
self.count += 1
self.wlanlist.append( lan )
def defaultSort( self, item ):
"""
defaultSort - The default sort function, given an item it returns the Received Signal
Strength Indicator, or RSSI of the item selected. This can be used to
sort by best to worst signal strength
"""
return item.rssi
def sortItems( self, sortFunction=None ):
"""
sortItems - This function will automatically sort the wlanlist based on the passed in
sort function. If there is no sort function provided, this code will utilize
the defaultSort function of sorting by the RSSI of the WLANs.
"""
if sortFunction == None:
self.wlanlist.sort( reverse=True, key=self.defaultSort )
else:
self.wlanlist.sort( reverse=True, key=sortFunction )
class WIFIScanner:
def __init__(self):
self.wlan = network.WLAN()
self.wlan.active(True)
def scanForWLANS( self, wlanList, iterations = 1):
'''
Scan the network looking for wifi networks.
Parameters:
wlanList - A location to store the data found
iterations - Number of times to scan prior to returning to the caller
'''
while( iterations > 0 ):
networks = self.wlan.scan()
for w in networks:
lan = WLAN( w[0].decode(),binascii.hexlify(w[1]).decode(),w[2],w[3],w[4],w[5])
wlanList.addItem(lan)
iterations -= 1
if iterations > 0:
time.sleep( 1 )
def scanForSpecificWLAN( self, ssid, iterations = 10 ):
'''
scanForSpecificWLAN - This routine scans for a specific SSID
We scan for a specific SSID, and when found return it. This routine
can be used to help graph the power of a specific SSID.
Parameters:
ssid - The specific SSID to scan the network for.
iterations - The number of times to scan prior to returning to the caller
'''
while( iterations > 0 ):
networks = self.wlan.scan()
for w in networks:
lan = WLAN( w[0].decode(),binascii.hexlify(w[1]).decode(),w[2],w[3],w[4],w[5])
if lan.ssid == ssid:
return lan
iterations -= 1
if iterations > 0:
time.sleep( 1 )
return None