-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridge.py
156 lines (122 loc) · 4.98 KB
/
Bridge.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
import sys
from pywifi.wifi import PyWiFi
from threading import Timer
from pywifi import const
class Bridge():
def __init__(self):
self.wifi = PyWiFi()
self.iface = self.wifi.interfaces()[0]
self.allProfiles = []
self.allWiFi_names = []
def scanWifi(self):
resultsSrc = self.iface.scan_results()
akmOfAllWiFi = []
for res in resultsSrc:
if bool(self.allWiFi_names.__contains__(res.ssid)) != True:
# Create JSON from the {string}
self.allWiFi_names.append(res.ssid)
self.allProfiles.append(res)
case = {
0: None,
1: 'WPA',
2: 'WPA_PSK',
3: 'WPA2',
4: 'WPA2_PSK',
5: 'UNKNOWN'
}
akm = case[res.akm[0]]
akmOfAllWiFi.append(akm)
print(self.allWiFi_names)
print(akmOfAllWiFi)
def getIface(self):
return self.iface
def connectTo(self, selectedSSID, password):
print(selectedSSID, password)
br = mainScan()
for WiFi_Profile in br.allProfiles:
# print('checking', WiFi_Profile.ssid)
if WiFi_Profile.ssid == selectedSSID:
# print('WiFi found')
# assert iface.status() in\
# [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]
WiFi_Profile.auth = const.AUTH_ALG_OPEN
WiFi_Profile.cipher = const.CIPHER_TYPE_CCMP
WiFi_Profile.akm = WiFi_Profile.akm
WiFi_Profile.key = None
if password != None or WiFi_Profile.akm != const.AKM_TYPE_NONE:
WiFi_Profile.key = password
minimalConnect(br, WiFi_Profile)
if br.getIface().status() != const.IFACE_CONNECTED or br.getIface().status() != const.IFACE_CONNECTING:
# Retrying connect again
# print(WiFi_Profile.ssid)
# print(WiFi_Profile.auth)
# print(WiFi_Profile.akm)
# print(WiFi_Profile.cipher)
# print(WiFi_Profile.bssid)
# print(WiFi_Profile.key)
if WiFi_Profile.akm != const.AKM_TYPE_NONE:
for auth in range(0, 2):
for cipher in range(0, 5):
WiFi_Profile.auth = auth
WiFi_Profile.cipher = cipher
WiFi_Profile.key = ""
# print("cipher {cipher}", cipher)
Timer(1000, minimalConnect(br, WiFi_Profile))
if br.getIface().status() == const.IFACE_CONNECTED or br.getIface().status() == const.IFACE_CONNECTING:
if br.getIface().status() == const.IFACE_CONNECTING:
print("CONNECTING")
break
else:
print("CONNECTED")
break
if br.getIface().status() == const.IFACE_CONNECTED:
print("CONNECTED")
break
print("CONNECTING")
def minimalConnect(br, WiFi_Profile):
if br.getIface().status() != const.IFACE_CONNECTED or br.getIface().status() != const.IFACE_CONNECTING:
br.getIface().disconnect()
# print("#############################################################")
# print(WiFi_Profile.ssid)
# print(WiFi_Profile.akm)
# print(WiFi_Profile.auth)
# print(WiFi_Profile.cipher)
# print(WiFi_Profile.bssid)
# print(WiFi_Profile.key)
# print("#############################################################")
br.getIface().add_network_profile(WiFi_Profile)
br.getIface().connect(WiFi_Profile)
# print(br.getIface().status())
# Run iface.scan() and after that wait 10 seconds
def mainScan():
br = Bridge()
br.getIface().scan()
Timer(10.0, br.scanWifi).run()
return br
def mainConnect():
# Play by command line args
selectedSSID = sys.argv[2]
try:
password = sys.argv[3]
except:
password = None
Bridge().connectTo(selectedSSID, password)
def mainStatus():
# Check if connected to any WiFi network
iface_mode = Bridge().getIface().status()
cases = {
const.IFACE_DISCONNECTED: 'DISCONNECTED',
const.IFACE_SCANNING: 'SCANNING',
const.IFACE_INACTIVE: 'INACTIVE',
const.IFACE_CONNECTING: 'CONNECTING',
const.IFACE_CONNECTED: 'CONNECTED'
}[iface_mode]
print(cases)
# Like switch case
cases = {
'-scan': mainScan,
'-connect': mainConnect,
'-status': mainStatus
}
runCase = cases[sys.argv[1]]
runCase()