-
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathenable-wifi-ap
More file actions
executable file
·185 lines (163 loc) · 5.03 KB
/
Copy pathenable-wifi-ap
File metadata and controls
executable file
·185 lines (163 loc) · 5.03 KB
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
#!/bin/bash
#
# Enable TinyPilot WiFi access point.
# Exit on first failure.
set -e
# Echo commands before executing them, by default to stderr.
set -x
# Exit on unset variable.
set -u
if (( "${EUID}" != 0 )); then
>&2 echo 'This script requires root privileges.'
>&2 echo 'Please re-run with sudo:'
>&2 echo " sudo $0 $*"
>&2 exit 1
fi
NETWORK_NAME='TinyPilotWiFi'
NETWORK_COUNTRY='US'
print_help() {
cat <<EOF
Usage: ${0##*/} [--help] [--password NETWORK_PASSWORD] [--ssid NETWORK_NAME] [--country NETWORK_COUNTRY]
Enable TinyPilot WiFi access point.
--help Display this help and exit.
--password NETWORK_PASSWORD The network password. Must be 8–63 characters in
length. If not specified, you will be prompted for
a password.
--ssid NETWORK_NAME Optional. The network name. Defaults to: ${NETWORK_NAME}
--country NETWORK_COUNTRY Optional. The network country in ISO 3166-1 alpha-2 format.
See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
Defaults to: ${NETWORK_COUNTRY}
Note: Running this script will clear any potentially existing custom static IP
configuration and also any potentially existing WiFi settings that might
have been set up previously.
EOF
}
# Parse command-line arguments.
NETWORK_PASSWORD=''
while (( "$#" > 0 )); do
case "$1" in
--help)
print_help
exit
;;
--password)
if (( "$#" < 2 )); then
shift;
break;
fi
NETWORK_PASSWORD="$2"
shift # For flag name.
shift # For flag value.
;;
--ssid)
if (( "$#" < 2 )); then
shift;
break;
fi
NETWORK_NAME="$2"
shift # For flag name.
shift # For flag value.
;;
--country)
if (( "$#" < 2 )); then
shift;
break;
fi
NETWORK_COUNTRY="$2"
shift # For flag name.
shift # For flag value.
;;
*)
>&2 echo "Unknown agrument: $1"
>&2 print_help
exit 1
;;
esac
done
readonly NETWORK_NAME
readonly NETWORK_COUNTRY
# Validate command-line arguments.
if [[ -z "${NETWORK_PASSWORD}" ]]; then
read -r -s -p 'Set a WiFi password: ' NETWORK_PASSWORD
fi
readonly NETWORK_PASSWORD
if [[ -z "${NETWORK_PASSWORD}" ]] || \
(( "${#NETWORK_PASSWORD}" < 8 )) || \
(( "${#NETWORK_PASSWORD}" > 63 )); then
>&2 echo 'The WiFi password must be 8–63 characters in length'
>&2 print_help
exit 1
fi
if [[ -z "${NETWORK_NAME}" ]]; then
>&2 echo "The network name can't be empty"
>&2 print_help
exit 1
fi
if (( "${#NETWORK_COUNTRY}" != 2 )); then
>&2 echo "Invalid network country: ${NETWORK_COUNTRY}"
>&2 echo 'The country must be a 2 character country code (e.g., US)'
>&2 print_help
exit 1
fi
# Set country code (persists to /boot/firmware/cmdline.txt and applies via
# iw reg set).
raspi-config nonint do_wifi_country "${NETWORK_COUNTRY}"
# Enable the wireless radio in case it was previously disabled.
nmcli radio wifi on
readonly CONNECTION_NAME='tinypilot-wlan0'
# Ensure exactly one tinypilot-wlan0 connection exists (singleton).
CONN_COUNT="$(nmcli --get-values connection.uuid \
connection show "${CONNECTION_NAME}" 2>/dev/null \
| wc -l | tr -d ' ')"
readonly CONN_COUNT
if (( CONN_COUNT == 0 )); then
# No connection exists — create one. An SSID is required when creating a
# wifi connection.
nmcli connection add \
type wifi \
con-name "${CONNECTION_NAME}" \
ifname wlan0 \
wifi.ssid "${NETWORK_NAME}" \
connection.autoconnect yes \
connection.autoconnect-priority 0
elif (( CONN_COUNT > 1 )); then
# Duplicates — delete all and recreate.
nmcli connection delete "${CONNECTION_NAME}" 2>/dev/null || true
nmcli connection add \
type wifi \
con-name "${CONNECTION_NAME}" \
ifname wlan0 \
wifi.ssid "${NETWORK_NAME}" \
connection.autoconnect yes \
connection.autoconnect-priority 0
fi
# AP's static IP address in CIDR notation. NM uses this as the gateway
# address for wlan0 and as the subnet for its internal dnsmasq DHCP server.
readonly NETWORK_AP_IP_ADDRESS='192.168.50.1/24'
# Modify to AP mode. NM's shared mode automatically runs an internal dnsmasq
# instance for DHCP serving to AP clients.
# Avoid leaking the password in command tracing.
set +x
nmcli connection modify "${CONNECTION_NAME}" \
wifi.mode ap \
wifi.band bg \
wifi.channel 7 \
wifi.ssid "${NETWORK_NAME}" \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "${NETWORK_PASSWORD}" \
wifi-sec.psk-flags 0 \
ipv4.method shared \
ipv4.addresses "${NETWORK_AP_IP_ADDRESS}" \
ipv4.gateway "" \
ipv4.dns ""
set -x
# Return as soon as NM accepts the activation request, so the script doesn't
# block on activation-layer problems when the configuration has already been
# saved successfully.
nmcli --wait 0 connection up "${CONNECTION_NAME}"
cat <<EOF
TinyPilot is now running a WiFi access point with the following details:
SSID: ${NETWORK_NAME}
Country: ${NETWORK_COUNTRY}
IP Range: ${NETWORK_AP_IP_ADDRESS}
EOF