forked from gitbls/autoAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-autoAP
executable file
·262 lines (236 loc) · 7.54 KB
/
install-autoAP
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
#!/bin/bash
#
# Installs autoAP (Automatic Access Point if no WiFi connection)
#
function askyn() {
local ans
echo -n "$1" '[y/n]? ' ; read $2 ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
fdhcpcd=1
echo ""
echo "Install autoAP into the current running system"
if [ "$(systemctl is-active systemd-networkd)" != "active" ]
then
echo ""
echo "? This system is not configured to use systemd-networkd"
echo " You must switch to using systemd-networkd to use autoAP"
echo " You can use /usr/local/bin/rpi-networkconfig to reconfigure your networking"
echo " rpi-networkconfig will configure wlan0 and eth0 to be DHCP-enabled"
echo " This can be done after install-autoAP has completed."
echo ""
if ! askyn "Do you want to continue with autoAP installation"
then
exit
fi
fdhcpcd=0
fi
echo ""
echo -n "SSID for Access Point mode: "
apssid=$(read ans; echo $ans)
echo -n "Password for Access Point mode: "
appsk=$(read ans; echo $ans)
echo -n "IPV4 address for Access Point mode [192.168.16.1]: "
apip=$(read ans; echo $ans)
[ "$apip" == "" ] && apip="192.168.16.1"
apssid="${apssid//\"/}" # Remove double quotes from SSID and password
appsk="${appsk//\"/}"
#
# Get existing WiFi configuration
#
wpafn="/etc/wpa_supplicant/wpa_supplicant.conf"
[ ! -f "$wpafn" -a -f "/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" ] && wpafn="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf"
if [ -f "$wpafn" ]
then
country=$(grep country= $wpafn | (IFS='='; read a b ; echo $b))
ssid=$(grep ssid= $wpafn | (IFS='='; read a b ; echo $b))
psk=$(grep psk= $wpafn | (IFS='='; read a b ; echo $b))
fi
if [ "$country" == "" -o "$ssid" == "" -o "$psk" == "" ]
then
echo ""
echo "% No appropriate wpa_supplicant configuration found, please provide..."
echo ""
echo -n "Your Country [US]: "
country=$(read ans; echo $ans)
[ "$country" == "" ] && country="US"
echo -n "Your WiFi SSID: "
ssid=$(read ans; echo $ans)
echo -n "Your WiFi password: "
psk=$(read ans; echo $ans)
fi
ssid="${ssid//\"/}" # Remove double quotes
psk="${psk//\"/}"
echo ""
echo " autoAP Configuration"
echo " Access Point SSID: $apssid"
echo " Access Point password: $appsk"
echo " Access Point IP addr: $apip"
echo " Your WiFi country: $country"
echo " Your WiFi SSID: $ssid"
echo " Your WiFi password: $psk"
echo ""
if ! askyn "Are you ready to proceed"
then
echo ""
echo "% No changes have been made to your system"
exit 0
fi
echo ""
echo "Renaming $wpafn"
echo " to ${wpafn}-orig to avoid future confusion..."
[ -f ${wpafn}-orig ] && rm -f ${wpafn}-orig.bak && mv ${wpafn}-orig ${wpafn}-orig.bak
mv $wpafn ${wpafn}-orig
wpafn="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" # Always write to this config file
echo ""
echo "Creating $wpafn..."
cat > $wpafn <<EOF
country=$country
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
network={
priority=10
ssid="$ssid"
psk="$psk"
}
### autoAP access point ###
network={
ssid="$apssid"
mode=2
key_mgmt=WPA-PSK
psk="$appsk"
frequency=2462
}
EOF
echo ""
echo "Creating WiFi network files in /etc/systemd/network..."
[ -f /etc/systemd/network/11-wlan0.network ] && mv /etc/systemd/network/11-wlan0.network /etc/systemd/network/.11-wlan0.network.bak
[ -f /etc/systemd/network/11-wlan0.network~ ] && rm -f /etc/systemd/network/11-wlan0.network~
cat > /etc/systemd/network/11-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
DHCP=ipv4
[DHCP]
RouteMetric=20
UseDomains=yes
EOF
[ -f /etc/systemd/network/12-wlan0AP.network ] && mv /etc/systemd/network/12-wlan0AP.network /etc/systemd/network/.12-wlan0AP.network.bak
cat > /etc/systemd/network/12-wlan0AP.network <<EOF
[Match]
Name=wlan0
[Network]
DHCPServer=yes
Address=$apip/24
EOF
echo ""
echo "Creating /etc/systemd/system/[email protected] configuration file..."
[ -f /etc/systemd/network/[email protected] ] && mv /etc/systemd/network/[email protected] /etc/systemd/network/[email protected]
cat > /etc/systemd/system/[email protected] <<EOF
[Unit]
Description=autoAP Automatic Access Point When No WiFi Connection ([email protected])
#After=network.target network-online.target wpa_supplicant@%i.service sys-subsystem-net-devices-%i.device
Before=wpa_supplicant@%i.service
BindsTo=wpa_supplicant@%i.service
[Service]
Type=simple
ExecStart=/usr/local/bin/autoAP.sh start %I
Restart=on-failure
TimeoutSec=1
[Install]
WantedBy=multi-user.target
EOF
#
# set up wpa-autoap-restore service
# Needed in the case that
# * 11-wlan0.network is in the disabled state (there is a client connected)
# * wpa-autoap@wlan0 service is not enabled at reboot
# * system reboots
# * 11-wlan0.network still in the disabled state, so no WiFi connection
# This service should always be enabled. VERY low cost!
#
echo "Creating /etc/systemd/system/wpa-autoap-restore.service configuration file..."
[ -f /etc/systemd/system/wpa-autoap-restore.service ] && mv -f /etc/systemd/system/wpa-autoap-restore.service /etc/systemd/system/wpa-autoap-restore.service.bak
cat > /etc/systemd/system/wpa-autoap-restore.service <<EOF
[Unit]
Description=Restore wpa-autoap configuration (wpa-autoap-restore.service)
DefaultDependencies=no
After=local-fs-pre.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c '[ -x /usr/local/bin/autoAP.sh ] && /usr/local/bin/autoAP.sh reset'
[Install]
WantedBy=multi-user.target
EOF
systemctl enable wpa-autoap-restore
echo ""
echo "Creating /usr/local/bin/autoAP-local.sh..."
[ -f /usr/local/bin/autoAP-local.sh ] && mv /usr/local/bin/autoAP-local.sh /usr/local/bin/autoAP-local.sh.bak
cat > /usr/local/bin/autoAP-local.sh <<EOF
#!/bin/bash
# \$1 has either "Client" or "AccessPoint"
logmsg () {
[ \$debug -eq 0 ] && logger --id=\$\$ "\$1"
}
[ -f /usr/local/bin/autoAP.conf ] && source /usr/local/bin/autoAP.conf || debug=0
case "\$1" in
Client)
logmsg "/usr/local/bin/autoAP-local: Client"
## Add your code here that runs when the Client WiFi is enabled
;;
AccessPoint)
logmsg "/usr/local/bin/autoAP-local: Access Point"
## Add your code here that runs when the Access Point is enabled
;;
esac
EOF
chmod 755 /usr/local/bin/autoAP-local.sh
echo ""
systemctl daemon-reload
echo "Modifying WPA services..."
echo "Enabling wpa_supplicant@wlan0..."
systemctl enable wpa_supplicant@wlan0
echo "Disabling (vanilla) wpa_supplicant..."
systemctl disable wpa_supplicant
echo ""
echo "Enabling wpa-autoap@wlan0 service"
echo "If you don't want it to be enabled, do: sudo systemctl disable wpa-autoap@wlan0"
systemctl enable wpa-autoap@wlan0
echo ""
echo "Creating /usr/local/bin/autoAP.conf..."
[ -f /usr/local/bin/autoAP.conf ] && mv /usr/local/bin/autoAP.conf /usr/local/bin/autoAP.conf.bak
cat > /usr/local/bin/autoAP.conf <<EOF
#
# enablewait
# In AP mode, number of seconds to wait before retrying regular WiFi connection
#
enablewait=300
#
# disconnectwait
# number of seconds to wait after last AP client disconnects before retrying regular WiFi connection
#
disconnectwait=20
#
# debug logging
# 0:debug logging on
# 1:debug logging off
#
debug=0
EOF
echo ""
echo "% Review the settings in /usr/local/bin/autoAP.conf"
if [ $fdhcpcd -eq 0 ]
then
echo ""
echo "** Reminder: Your system must use systemd-networkd to work with autoAP"
echo " Look at /usr/local/bin/rpi-networkconfig"
echo " Do this before rebooting your system"
echo ""
fi
echo ""
echo "autoAP installation complete"
echo "Please reboot the system for the configuration changes to take effect"