-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_testing.py
68 lines (58 loc) · 2.4 KB
/
wifi_testing.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
import subprocess
import platform
def get_wifi_info():
"""gets wifi info and returns a dictionary
Parameters
----------
None
Returns
-------
dictionary
a dictionary of the wifi information including ssid and such
"""
#macOS get ssid
if platform.system() == 'Darwin':
wifi = subprocess.check_output(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport','-I']).decode('utf-8', 'ignore')
datalist = wifi.split("\n")
datalist = [x.strip() for x in datalist if x if 'SSID' in x and 'BSSID' not in x]
datalist = datalist[0].split(':')
data = datalist[1].strip()
#windows get ssid
if platform.system() == 'Windows':
wifi = subprocess.check_output(['netsh', 'WLAN', 'show', 'interfaces']).decode('utf-8', 'ignore')
# turn lines into a list by splitting by \r\n
datalist = wifi.split("\r\n")
# remove any empty strings (multiple cases of \r\n)
datalist = [x for x in datalist if x]
newlist = []
# Problem with bssid and physical address. They use colons to separate
# each element. Need to replace those particular colons with periods.
# Then we can split each line by the colon and add all those into the
# list.
for element in datalist:
if 'BSSID' in element or 'Physical address' in element:
newelement = element[:-17]
tempelement = element[-17:].replace(":", ".")
element = newelement + tempelement
element = element.split(':')
# remove whitespace from before and after text
element = [x.strip() for x in element]
# remove any empty strings after removing whitespaces
element = [x for x in element if x]
# now add it to the newlist
newlist.extend(element)
# now remove useless first element
newlist = newlist[1:]
data = newlist[newlist.index('SSID')+1]
#linux get ssid
elif platform.system() == 'Linux':
#wifi = subprocess.check_output(['netsh', 'WLAN', 'show', 'interfaces'])
wifi = subprocess.check_output('iwgetid').decode('utf-8', 'ignore')
data = wifi.split('"')[1]
# return ssid
return data
def main():
test = get_wifi_info()
print(f"Wifi name is {test}")
if __name__ == "__main__":
main()