-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi_config
executable file
·56 lines (48 loc) · 2.12 KB
/
wifi_config
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
#!/usr/bin/env python3
import argparse
import os
import sys
def parseArgs():
parser = argparse.ArgumentParser(
description=('Configure /boot/wpa_supplicant.conf ' +
'with a given network name and password such that the pifi will automatically ' +
'attempt to join that network upon its next reboot. Can be run from laptop or ' +
'pi. If run from laptop, must specify --device-path. See: ' +
'https://raspberrypi.stackexchange.com/a/57023'),
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--wifi-network-name', dest='wifi_network_name', action='store', required = True,
help='Name of the network to join')
parser.add_argument('--wifi-password', dest='wifi_password', action='store', required = True,
help='Password of the network to join')
parser.add_argument('--wifi-country-code', dest='wifi_country_code', action='store', default='US',
help='Your ISO-3166-1 two-letter country code. See: ' +
'https://www.iso.org/obp/ui/#search')
parser.add_argument('--device-path', dest='device_path', action='store', default='/boot',
help='Mounted volume path after SD card has been imaged. Only applicable / useful if you are running ' +
'this script on a laptop / desktop to image the SD card for the first time. If running this script on a ' +
'raspberry pi, do not use this argument.')
args = parser.parse_args()
args.device_path = args.device_path.rstrip('/')
return args
def setupWifi():
f = None
try:
f = open(f"{args.device_path}/wpa_supplicant.conf", "w")
except PermissionError:
print("This script must be run as sudo", file = sys.stderr)
sys.exit(1)
f.write(f"""country={args.wifi_country_code}
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={{
ssid="{args.wifi_network_name}"
psk="{args.wifi_password}"
key_mgmt=WPA-PSK
}}""")
f.close()
if os.getuid() != 0:
print("This script must be run as sudo", file = sys.stderr)
sys.exit(1)
args = parseArgs()
setupWifi()