This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup_network.sh
executable file
·375 lines (310 loc) · 10.3 KB
/
setup_network.sh
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
#
# Copyright 2016 Philip J Freeman <elektron@halo.nu>
#
#
# This file is part of Signalk-java
#
# FreeBoard is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FreeBoard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FreeBoard. If not, see <http://www.gnu.org/licenses/>.
#
#
# This is a setup script to automate the installation of Signalk-java
# On Raspbian stretch.
#
# Use bash "strict mode" - http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
if [[ $# -lt 2 ]] ; then
echo 'Correct usage is: setup_network.sh [HOSTNAME] [hotspot (Y|N)] [Wifi SSID] [Wifi PASSWORD]'
echo ' eg: setup_network.sh freeboard Y freeboard passit'
exit 1
fi
HOSTNAME=${1}
DO_HOTSPOT_NETWORK=${2}
HOTSPOT_NETWORK_WIFI_SSID=${3:-${HOSTNAME}}
HOTSPOT_NETWORK_WIFI_PASS=${4:-${HOTSPOT_NETWORK_WIFI_SSID}}
# check the pass is > 8
if [ ${#HOTSPOT_NETWORK_WIFI_PASS} -lt 8 ];then
HOTSPOT_NETWORK_WIFI_PASS=${HOTSPOT_NETWORK_WIFI_PASS}${HOTSPOT_NETWORK_WIFI_PASS}
fi
if [ ${#HOTSPOT_NETWORK_WIFI_PASS} -lt 8 ];then
HOTSPOT_NETWORK_WIFI_PASS=${HOTSPOT_NETWORK_WIFI_PASS}${HOTSPOT_NETWORK_WIFI_PASS}
fi
#echo " running: setup_network.sh $HOSTNAME $DO_HOTSPOT_NETWORK $HOTSPOT_NETWORK_WIFI_SSID $HOTSPOT_NETWORK_WIFI_PASS"
#exit 1
# Boat Network Defaults
HOTSPOT_NETWORK_IFACE=wlan0
HOTSPOT_ROAM_IFACE=wlan1
HOTSPOT_NETWORK_ADDRESS=192.168.0.1
HOTSPOT_NETWORK_NETMASK=255.255.255.0
HOTSPOT_NETWORK_MIN_DHCP=192.168.0.10
HOTSPOT_NETWORK_MAX_DHCP=192.168.0.128
HOTSPOT_NETWORK_WIFI_CHAN=10
# helper functions
HAVE_RUN_APT_UPDATE=N
function ensure_package_installed()
{
DO_INSTALL=N
if ! dpkg -s ${1} > /dev/null 2>&1; then
DO_INSTALL=Y
else
STATUS=$(dpkg -s ${1} | grep ^Status: | head -1 | awk '{print $4}')
if [ "${STATUS}" != "installed" ]; then
DO_INSTALL=Y
fi
# TODO: handle minimum version argument?
# VERSION=$(dpkg -s ${1} | grep ^Version: | head -1 | awk '{print $2}')
# if dpkg --compare-versions ${VERSION} lt ${2}; then
fi
if [ "${DO_INSTALL}" == "Y" ]; then
if [ "${HAVE_RUN_APT_UPDATE}" == "N" ]; then
sudo apt-get update
HAVE_RUN_APT_UPDATE=Y
fi
sudo apt-get --assume-yes install ${1}
fi
}
function system_enable_service()
{
if ! sudo systemctl is-enabled ${1}; then
sudo systemctl enable ${1}
fi
}
function system_disable_service()
{
if sudo systemctl is-enabled ${1}; then
sudo systemctl disable ${1}
fi
}
function system_stop_service()
{
if sudo systemctl is-active ${1}; then
sudo systemctl stop ${1}
fi
}
# TODO: allow user to override the boat network settings, and cache the overrides
# for next time
set -x # Turn on debug output
DO_RESTART_SERVICE=N
#DO_REBOOT_SYSTEM=N
DO_REBOOT_SYSTEM=Y
DO_RESTART_DNSMASQ=N
DO_RESTART_HOSTAPD=N
###########
STATIC_HOSTS_ENTRIES="127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters"
###########
###########
HOSTAPD_DEFAULT="DAEMON_CONF=\"/etc/hostapd/hostapd.conf\""
HOSTAPD_CONFIG="interface=${HOTSPOT_NETWORK_IFACE}
driver=nl80211
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
ssid=${HOTSPOT_NETWORK_WIFI_SSID}
hw_mode=g
channel=${HOTSPOT_NETWORK_WIFI_CHAN}
ieee80211n=1
wpa=1
wpa_passphrase=${HOTSPOT_NETWORK_WIFI_PASS}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
wmm_enabled=1"
###########
###########
DNSMASQ_CONFIG="interface=wlan0
dhcp-range=${HOTSPOT_NETWORK_MIN_DHCP},${HOTSPOT_NETWORK_MAX_DHCP},12h"
###########
###########
NETWORK_INTERFACES="
# This file is managed by signalk-java
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
metric 20
allow-hotplug ${HOTSPOT_NETWORK_IFACE}
iface ${HOTSPOT_NETWORK_IFACE} inet static
address ${HOTSPOT_NETWORK_ADDRESS}
netmask ${HOTSPOT_NETWORK_NETMASK}
allow-hotplug ${HOTSPOT_ROAM_IFACE}
iface ${HOTSPOT_ROAM_IFACE} inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
metric 10
iface default inet dhcp
"
###########
###########
RC_LOCAL="
#!/bin/sh -e
# This file is managed by signalk-java
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf \"My IP address is %s\n\" \"\$_IP\"
fi
#setup routing
sudo sh -c \"echo 1 > /proc/sys/net/ipv4/ip_forward\"
sudo iptables-restore < /etc/iptables.ipv4.nat
exit 0
"
###########
###########
HOSTS_FILE="
# This file is managed by ${0}
${STATIC_HOSTS_ENTRIES}
${HOTSPOT_NETWORK_ADDRESS} ${HOSTNAME} a.${HOSTNAME} b.${HOSTNAME} c.${HOSTNAME} d.${HOSTNAME}
127.0.1.1 ${HOSTNAME}
"
###########
# Verify our running environment
## Raspbian Lite does not have lsb-release installed by default
ensure_package_installed "lsb-release"
LSB_ID=$(lsb_release -is)
LSB_CODENAME=$(lsb_release -cs)
## check lsb_release for Raspbian stretch
if [ "${LSB_ID}" != "Raspbian" -o "${LSB_CODENAME}" != "stretch" ]; then
echo "distro ${LSB_ID} ${LSB_CODENAME} is not supported."
exit 1
fi
# routing
sudo apt-get install -y ifmetric
## check running user is 'pi'
if [ "$(id -nu)" != "pi" ]; then
echo "ERROR: script must be run as the 'pi' user"
exit 1
fi
## change to HOME
cd ${HOME}
# change the system's hostname
sudo cp /etc/hostname /etc/hostname.bak
echo "${HOSTNAME}" | sudo tee /etc/hostname > /dev/null
if ! diff /etc/hostname.bak /etc/hostname > /dev/null; then
sudo hostname ${HOSTNAME}
sudo hostnamectl set-hostname ${HOSTNAME}
sudo systemctl restart avahi-daemon
DO_REBOOT_SYSTEM=Y
fi
# Optionally Setup Boat Network
if [ "${DO_HOTSPOT_NETWORK}" == "Y" ]; then
## TODO: validate wifi device supports master mode
## setup hosts file
sudo cp /etc/hosts /etc/hosts.bak
echo "${HOSTS_FILE}" | sudo tee /etc/hosts
if ! diff /etc/hosts.bak /etc/hosts > /dev/null; then
DO_RESTART_DNSMASQ=Y
fi
## save a backup original rc.local file
if [ ! -e /etc/rc.local.orig ]; then
sudo cp /etc/rc.local /etc/rc.local.orig
fi
## save a backup original interfaces file
if [ ! -e /etc/network/interfaces.orig ]; then
sudo cp /etc/network/interfaces /etc/network/interfaces.orig
fi
## Add rc.local
if ! grep "^# This file is managed by signalk-java" /etc/rc.local > /dev/null; then
echo "${RC_LOCAL}" | sudo tee /etc/rc.local
fi
## Add interface config
if ! grep "^# This file is managed by signalk-java" /etc/network/interfaces > /dev/null; then
echo "${NETWORK_INTERFACES}" | sudo tee /etc/network/interfaces
fi
## ensure required packages are installed
ensure_package_installed dnsmasq
ensure_package_installed hostapd
## configure dnsmasq
if [ "${DNSMASQ_CONFIG}" != "$(cat /etc/dnsmasq.d/signalk-java.conf)" ]; then
echo "${DNSMASQ_CONFIG}" | sudo tee /etc/dnsmasq.d/signalk-java.conf
DO_RESTART_DNSMASQ=Y
fi
## configure hostapd
if [ "${HOSTAPD_CONFIG}" != "$(cat /etc/hostapd/hostapd.conf)" ]; then
echo "${HOSTAPD_CONFIG}" | sudo tee /etc/hostapd/hostapd.conf
DO_RESTART_HOSTAPD=Y
fi
if [ "${HOSTAPD_DEFAULT}" != "$(cat /etc/default/hostapd)" ]; then
echo "${HOSTAPD_DEFAULT}" | sudo tee /etc/default/hostapd
DO_RESTART_HOSTAPD=Y
fi
# setup routing
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
sudo iptables -A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o wlan1 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
# insert into rclocal
#iptables-restore < /etc/iptables.ipv4.nat
## enable network daemons
sudo systemctl unmask hostapd.service
system_enable_service "hostapd" # Note: Due to a bug in debian stretch, this
# enable service triggers each time you run
# the script. This does not cause a failure
# other than the output:
#
# + sudo systemctl is-enabled hostapd
# Failed to get unit file state for hostapd.service: No such file or directory
# - https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=751638
system_enable_service "dnsmasq"
## do network daemon restarts, if requested
if [ "${DO_RESTART_DNSMASQ}" == "Y" ]; then
sudo systemctl restart dnsmasq
fi
if [ "${DO_RESTART_HOSTAPD}" == "Y" ]; then
sudo systemctl restart hostapd
fi
else
## bring down wireless
if sudo ifquery --state ${HOTSPOT_NETWORK_IFACE} > /dev/null; then
sudo ifdown ${HOTSPOT_NETWORK_IFACE}
fi
## disable and stop network daemons
system_stop_service "hostapd" # Note: see note above ^^ near
# system_enable_service "hostapd"
system_disable_service "hostapd"
system_stop_service "dnsmasq"
system_disable_service "dnsmasq"
if [ -e /etc/rc.local.orig ]; then
sudo mv /etc/rc.local.orig /etc/rc.local
fi
## Revert to default network settings
if [ -e /etc/network/interfaces.orig ]; then
sudo mv /etc/network/interfaces.orig /etc/network/interfaces
fi
## setup hosts file
sudo cp /etc/hosts /etc/hosts.bak
sudo tee /etc/hosts << EOF
# This file is managed by ${0}
${STATIC_HOSTS_ENTRIES}
127.0.1.1 ${HOSTNAME}
EOF
fi # End if DO_HOTSPOT_NETWORK
# make setup script in homedir a symlink to script in source
if [ ! -L ~/setup_network.sh ]; then
# rm ~/setup_raspbian.sh
ln -s ~/signalk-java/setup_network.sh ~/setup_network.sh
fi
set +x # Turn off debug output
echo "The script has completed successfully."
if [ "${DO_REBOOT_SYSTEM}" == "Y" ]; then
echo
echo "Press ENTER to reboot or CTRL-c to cancel"
# wait for user to hit enter
read -t 5
sudo shutdown -r now
fi