-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
wifi_password.py
executable file
·172 lines (125 loc) · 4.88 KB
/
wifi_password.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
#!/usr/bin/env python3
"""
Quickly fetch your WiFi password and if needed, generate a QR code
of your WiFi to allow phones to easily connect
by Siddharth Dushantha
"""
import pathlib
import sys
import subprocess
import argparse
from shutil import which
import re
import os
import qrcode
import colorama
__version__ = "1.1.1"
def run_command(command: str) -> str:
"""
Runs a given command using subprocess module
"""
env = os.environ.copy()
env["LANG"] = "C"
output, _ = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True, env=env).communicate()
return output.decode("utf-8").rstrip("\r\n")
def print_error(text) -> None:
"""
Shows an error message and exits the program with the status code 1
"""
print(f"ERROR: {text}", file=sys.stderr)
sys.exit(1)
def get_ssid() -> str:
"""
Get the SSID which the computer is currently connected to
"""
if sys.platform == "darwin":
airport = pathlib.Path("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport")
if not airport.is_file():
print_error(f"Can't find 'airport' command at {airport}")
ssid = run_command(f"{airport} -I | awk '/ SSID/ {{print substr($0, index($0, $2))}}'")
elif sys.platform == "linux":
if which("nmcli") is None:
print_error("Network Manager is required to run this program on Linux.")
ssid = run_command("nmcli -t -f active,ssid dev wifi | egrep '^yes:' | sed 's/^yes://'")
elif sys.platform == "win32":
ssid = run_command("netsh wlan show interfaces | findstr SSID")
if ssid == "":
print_error("SSID was not found")
ssid = re.findall(r"[^B]SSID\s+:\s(.*)", ssid)[0]
return ssid
def get_password(ssid: str) -> str:
"""
Gets the password for a given SSID
"""
if ssid == "":
print_error("SSID is not defined")
if sys.platform == "darwin":
password = run_command(f"security find-generic-password -l \"{ssid}\" -D 'AirPort network password' -w")
elif sys.platform == "linux":
# Check if the user is running with super user privilages
if os.geteuid() != 0:
password = run_command(f"sudo nmcli -s -g 802-11-wireless-security.psk connection show '{ssid}'")
else:
password = run_command(f"nmcli -s -g 802-11-wireless-security.psk connection show '{ssid}'")
elif sys.platform == "win32":
password = run_command(f"netsh wlan show profile name=\"{ssid}\" key=clear | findstr Key")
password = re.findall(r"Key Content\s+:\s(.*)", password)[0]
if password == "":
print_error("Could not find password")
return password
def generate_qr_code(ssid: str, password: str, path: str, show_qr: bool) -> None:
"""
Generate a QR code based on the given SSID and password
"""
# Source: https://git.io/JtLIv
text = f"WIFI:T:WPA;S:{ssid};P:{password};;"
qr = qrcode.QRCode(version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4)
qr.add_data(text)
if show_qr:
# This will emulate support for ANSI escape sequences, which is needed
# in order to display the QR code on Windows
colorama.init()
qr.make()
qr.print_tty()
if path:
file_name = ssid.replace(" ", "_") + ".png"
if path == "STORE_LOCALLY":
path = file_name
try:
img = qr.make_image()
img.save(path)
except FileNotFoundError:
print_error(f"No such file/directory: '{path}'")
print(f"QR code has been saved to {path}")
def main() -> None:
parser = argparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument("--show-qr", "-show",
action="store_true",
default=False,
help="Show a ASCII QR code onto the terminal/console")
parser.add_argument("--save-qr", "-save",
metavar="PATH",
nargs="?",
const="STORE_LOCALLY",
help="Create the QR code and save it as an image")
parser.add_argument("--ssid", "-s",
help="Specify a SSID that you have previously connected to")
parser.add_argument("--version",
action="store_true",
help="Show version number")
args = parser.parse_args()
if args.version:
print(__version__)
sys.exit()
if args.ssid is None:
args.ssid = get_ssid()
password = get_password(args.ssid)
if args.show_qr or args.save_qr:
generate_qr_code(ssid=args.ssid, password=password, path=args.save_qr, show_qr=args.show_qr)
return
print(password)
if __name__ == "__main__":
main()