/
module_connect.py
204 lines (149 loc) · 5.48 KB
/
module_connect.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from subprocess import check_output
from datetime import datetime
from time import sleep
from utils import NL, confirm, try_call
from module_profiles import get_profile_list, \
generate_profile, \
add_profile, \
delete_profile
# ---
class Network:
def __init__ (self, BSSID, SSID, network_type, authentication, encryption, protocol, channel):
self.BSSID = BSSID
self.SSID = SSID
self.network_type = network_type
self.authentication = authentication
self.encryption = encryption
self.protocol = protocol
self.last_seen = datetime.now()
self.channel = channel
# ---
def update_signal_strength (self, signal_strength):
self.signal_strength = signal_strength
self.last_seen = datetime.now()
# ---
def display_connection_status ():
print()
connection_status = get_connection_status()
if connection_status['State'] == 'connected':
print('Connected to ' + connection_status['SSID'] + ' (' + connection_status['BSSID'] + ')')
else:
print('Disconnected.')
# ---
def get_connection_status ():
result = {}
output = str(check_output('netsh wlan show interface wi-fi'))
for line in output.split('\\r\\n'):
pair = line.split(' : ')
if len(pair) == 2:
result[pair[0].strip()] = pair[1].strip()
return result
# ---
def get_network_list ():
networks = {}
# Get all nearby networks using netsh
netsh_output = str(check_output('netsh wlan show networks mode=bssid'))
# For each SSID
for result in netsh_output.split('\\r\\nSSID')[1:]:
# Split the result into key/value pairs
pairs = result.split('\\r\\n')
SSID = pairs[0].split(' : ')[1].strip()
network_type = pairs[1].split(' : ')[1].strip()
authentication = pairs[2].split(' : ')[1].strip()
encryption = pairs[3].split(' : ')[1].strip()
# For each BSSID
for sub_result in result.split('BSSID')[1:]:
# Split the result into key/value pairs
sub_pairs = sub_result.split('\\r\\n')
BSSID = sub_pairs[0].split(' : ')[1].strip()
signal = sub_pairs[1].split(' : ')[1].strip()
protocol = sub_pairs[2].split(' : ')[1].strip()
channel = sub_pairs[3].split(' : ')[1].strip()
# Record the network details
networks[BSSID] = Network(BSSID, SSID, network_type, authentication, encryption, protocol, channel)
networks[BSSID].update_signal_strength(signal)
return networks
# ---
def autocomplete_network (SSID):
for network in get_network_list().values():
if network.SSID.lower().startswith(SSID.lower()):
return network
# ---
def connect_to_network (name):
print()
# Are we already connected?
connection_status = get_connection_status()
if connection_status['State'] == 'connected':
print('Already connected to ' + connection_status['SSID'] + ' (' + connection_status['BSSID'] + ')')
if confirm('Disconnect?'):
disconnect_from_network()
print()
else:
return
# Is there a nearby network with this SSID?
target_network = autocomplete_network(name)
if not target_network:
print('Could not locate ' + name)
return
print('Found network ' + target_network.SSID + ' (' + target_network.BSSID + ')')
print('Security: ' + target_network.authentication + ' (' + target_network.encryption + ')' + NL)
# Does a profile exist for this SSID?
for profile_SSID, profile_name in get_profile_list().items():
if profile_SSID.lower() == target_network.SSID.lower():
if confirm('Use existing profile?'):
# Attempt to connect using the existing profile
print()
attempt_connection(profile_name)
return
else:
print(NL + 'This will overwrite the existing profile.')
if confirm('Are you sure?'):
# Delete the existing profile
delete_profile(profile_name)
print()
else:
return
# Generate a new profile
profile = generate_profile(target_network)
# Add the network password, if required
if target_network.authentication not in ['None', 'Open']:
password = input('Password required: ')
profile = profile.format(password)
# Attempt to connect using the new profile
command_result = add_profile(profile)
if not command_result['Success']:
print(command_result['Output'])
return
attempt_connection(target_network.SSID)
# ---
def attempt_connection (profile_name):
print('Sending connection request...')
command_result = try_call(['netsh', 'wlan', 'connect', profile_name])
if not command_result['Success']:
print(command_result['Output'])
try:
# Check if the request was successful
for i in range(5):
sleep(1)
if get_connection_status()['State'] == 'connected':
print('Connected.')
return
except KeyboardInterrupt:
pass
print('Request timed out.' + NL)
print('This may mean the network is no longer available or the network password is incorrect')
if confirm('Delete associated profile?'):
delete_profile(profile_name)
# ---
def disconnect_from_network ():
print()
connection_status = get_connection_status()
if connection_status['State'] == 'disconnected':
print('Not connected to a network.')
return
command_result = try_call('netsh wlan disconnect')
if command_result['Success']:
print('Disconnected from ' + connection_status['SSID'])
else:
print('Disconnect failed.')
print(command_result['Output'])