-
Notifications
You must be signed in to change notification settings - Fork 5
/
wifi-share.py
executable file
·295 lines (275 loc) · 12 KB
/
wifi-share.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
from __future__ import print_function
import argparse
import subprocess
import sys
import os
from subprocess import Popen, PIPE, STDOUT
from collections import OrderedDict
import qrcode
import qrcode.image.svg
import PIL
from PyInquirer import prompt
from huepy import *
import platform
verbose = True
ascii_art='''
__ ___ ______ _ _____ _
\ \ / (_) | ____(_) / ____| |
\ \ /\ / / _ ______| |__ _ | (___ | |__ __ _ _ __ ___
\ \/ \/ / | |______| __| | | \___ \| '_ \ / _` | '__/ _ \\
\ /\ / | | | | | | ____) | | | | (_| | | | __/
\/ \/ |_| |_| |_| |_____/|_| |_|\__,_|_| \___|
'''
def log(message):
if verbose:
print(message, file=sys.stderr)
class ProcessError(Exception):
def __init__(self, message=''):
self.message = message
def __str__(self):
return self.message
# Execute provided command(s).
# In case of multiple commands, pipe them.
def execute(commands, stdout=PIPE, stdin=PIPE, stderr=STDOUT):
logged = False
input = stdin
if not any(isinstance(el, list) for el in commands): # if we have only one command
commands = [commands]
else:
log(run(bold('Running: ') + ' | '.join([' '.join(command) for command in commands])))
logged = True
for command in commands:
if logged:
log(run('... ' + ' '.join(command)))
else:
log(run(bold('Running: ') + ' '.join(command)))
process = Popen(command, stdout = stdout, stdin = input, stderr = stderr)
input = process.stdout
out, err = process.communicate();
rc = process.returncode
if out != None:
out = out.decode("utf-8").rstrip()
if rc != 0:
if err != None:
err = err.decode("utf-8").rstrip()
raise ProcessError(err)
else:
raise ProcessError(out)
return out
def escape(input_string):
translations = OrderedDict([('\\' , '\\\\'),
(':' , '\\:'),
(';' , '\\;'),
(',' , '\\,'),
('"' , '\\"')])
escaped = input_string
for k,v in translations.items():
escaped = escaped.replace(k, v)
return escaped
def fix_ownership(path): # Change the owner of the file to SUDO_UID
uid = os.environ.get('SUDO_UID')
gid = os.environ.get('SUDO_GID')
if uid is not None:
os.chown(path, int(uid), int(gid))
def create_QR_string(ssid = None, security = 'WPA', password = None):
if ssid != None:
if password != None:
return 'WIFI:T:WPA;S:' + escape(ssid) + ';P:' + escape(password) + ';;'
else:
return 'WIFI:S:' + escape(ssid) + ';;;'
return ''
def create_QR_object(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
return qr
def main():
global verbose
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=ascii_art)
parser.add_argument('-v', '--verbose', help = 'Enable verbose output.', action = 'store_true')
parser.add_argument('-i', '--image', help = 'Specify a filename for the generated QR code image. (.png or .svg).\
Default: [WIFINAME].svg.\
If -i/--image argument is not provided the QR code will be displayed\
on the console.', nargs='?', default = 'no-image')
parser.add_argument('-s', '--ssid', help = 'Specify the SSID you want the password of.\
Default: the SSID of the network you are currently connected.')
parser.add_argument('-p', '--password', help = 'Specify a desired password to be used instead of the stored one.')
parser.add_argument('-l', '--list', help = 'Display a list of stored Wi-Fi networks (SSIDs) to choose from.', action = 'store_true')
args = parser.parse_args()
if args.list and args.ssid:
print(bad('The -s/--ssid SSID and -l/--list are mutually exclusive arguments.'))
sys.exit(1)
verbose = args.verbose
wifi_name = args.ssid
connection_name = ''
system = platform.system()
if args.list:
available_networks = []
if system == 'Windows':
try:
output = execute(['netsh', 'wlan', 'show', 'profiles']).rstrip()
for line in output.splitlines():
if line.startswith(' All User Profile'):
available_networks.append(line.split(':')[1].lstrip())
if available_networks == []:
raise ProcessError
except ProcessError as e:
log(bad(e))
print(bad('Error getting Wi-Fi connections'))
sys.exit(1)
elif system == 'Darwin':
try:
output = execute([['defaults', 'read', '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences'],
['grep', 'SSIDString']])
for line in output.splitlines():
if line.startswith(' SSIDString ='):
available_networks.append(line.split('=')[1].lstrip()[:-1].replace('"', ''))
if available_networks == []:
raise ProcessError
except ProcessError as e:
log(bad(e))
print(bad('Error getting Wi-Fi connections'))
sys.exit(1)
else:
try:
output = execute([['nmcli', '--terse', '--fields', 'name,type', 'connection', 'show'],
['awk', '-F:', '/802-11-wireless/ {print $1}']])
connections = output.splitlines()
if connections == []:
raise ProcessError
available_networks = [execute([['nmcli', '--terse', 'connection', 'show', connection],
['awk', '-F:', '/802-11-wireless.ssid/ {print $2}']]) for connection in connections]
if available_networks == []:
raise ProcessError
except ProcessError as e:
log(bad(e))
print(bad('Error getting Wi-Fi connections'))
sys.exit(1)
questions = [
{
'type': 'list',
'name': 'network',
'message': 'SSID:',
'choices' : available_networks
}
]
answer = prompt(questions)
if answer == {}:
raise KeyboardInterrupt
wifi_name = answer['network']
if system == 'Linux':
connection_name = connections[available_networks.index(wifi_name)]
log(run('Retrieving the password for ' + green(wifi_name) + ' Wi-Fi'))
elif args.ssid == None:
try:
if system == 'Windows':
output = execute(['netsh', 'wlan', 'show', 'interfaces']).rstrip()
for line in output.splitlines():
if line.startswith(' SSID'):
wifi_name = line.split(':')[1].lstrip()
if wifi_name == None:
raise ProcessError
elif system == 'Darwin':
output = execute(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I'])
for line in output.splitlines():
if line.startswith(' SSID'):
wifi_name = line.split(':')[1].lstrip()
if wifi_name == None:
raise ProcessError
else:
wifi_name = execute([['nmcli', '--terse', '--fields', 'active,ssid', 'device', 'wifi'],
['awk', '-F:', '/yes/ {print $2}']])
if wifi_name == '':
raise ProcessError
connection_name = execute([['nmcli', '--terse', '-fields', 'name,type', 'connection', 'show', '--active'],
['awk', '-F:', '/802-11-wireless/ {print $1}']])
if connection_name == '':
raise ProcessError
except ProcessError as e:
log(bad(e))
print(bad('Error getting Wi-Fi name'))
print(que('Are you sure you are connected to a Wi-Fi network?'))
sys.exit(1)
log(good('You are connected to ' + green(wifi_name) + ' Wi-Fi'))
else:
log(run('Retrieving the password for ' + green(wifi_name) + ' Wi-Fi'))
wifi_password = ''
if args.password != None:
wifi_password = args.password
else:
try:
if system == 'Windows':
output = execute(['netsh', 'wlan', 'show', 'profile', wifi_name, 'key=clear']).rstrip()
for line in output.splitlines():
if line.startswith(' Key Content'):
wifi_password = line.split(':')[1].lstrip()
elif system == 'Darwin':
wifi_password = execute(['security', 'find-generic-password', '-wga', wifi_name])
else:
if connection_name == '':
output = execute([['nmcli', '--terse', '--fields', 'name,type', 'connection', 'show'],
['awk', '-F:', '/802-11-wireless/ {print $1}']])
connections = output.splitlines()
if connections == []:
raise ProcessError
if args.list:
connection_name = connections[available_networks.index(wifi_name)]
else:
for connection in connections:
ssid = execute([['nmcli', '--terse', 'connection', 'show', connection],
['awk', '-F:', '/802-11-wireless.ssid/ {print $2}']])
if ssid == wifi_name:
connection_name = connection
break
wifi_password = execute([['nmcli', '--terse', '--fields', '802-11-wireless-security.psk', '--show-secrets', 'connection', 'show', 'id', connection_name],
['awk', '-F:', '{print $2}']])
if wifi_password == None:
raise ProcessError
except (ProcessError, IOError) as e:
log(bad(e))
print(bad('Error getting Wi-Fi password'))
if e.__class__ == IOError:
if e.errno == 13:
print(que('Are you root?'))
elif e.errno == 2 and args.ssid != None:
print(que('Are you sure SSID is correct?'))
sys.exit(1)
if wifi_password != '':
log(good('The password is ' + green(wifi_password)))
data = create_QR_string(ssid = wifi_name, password = wifi_password)
else:
log(info('No password needed for this network.'))
data = create_QR_string(ssid = wifi_name)
qr = create_QR_object(data)
if args.image == 'no-image': # If user did not enter the -i/--image argument
qr.print_tty()
else:
img = qrcode.make(data)
if args.image == None: # If user selected the -i/--image argument, but did not give any filename
img = qrcode.make(data, image_factory=qrcode.image.svg.SvgPathFillImage)
filename = wifi_name + '.svg'
else: # If user specified a filename with the -i/--image argument
if args.image.endswith('.svg'):
img = qrcode.make(data, image_factory=qrcode.image.svg.SvgPathFillImage)
filename = args.image
elif args.image.endswith('.png'):
filename = args.image
img = qr.make_image(fill_color="black", back_color="white")
else:
img = qrcode.make(data, image_factory=qrcode.image.svg.SvgPathFillImage)
filename = args.image + '.svg'
img.save(filename)
if system == 'Linux':
fix_ownership(filename)
print(good('Qr code drawn in '+filename))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
log('\nk bye')
sys.exit(1)