-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwiperf_switcher
executable file
·308 lines (271 loc) · 9.52 KB
/
wiperf_switcher
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
#! /bin/bash
#
# wiperf_switcher script to switch wiperf on/off
#
# Written by Nigel Bowden <[email protected]>.
#
set -e
NAME=wiperf_switcher
DESC="Script to switch wiperf mode on/off"
STATUS_FILE="/etc/wlanpi-state"
HOME_DIR="/usr/share/wiperf"
USERNAME="root"
CFG_DIR="/etc/wiperf"
CFG_FILE="$CFG_DIR/config.ini"
LOG_FILE="/var/log/wiperf_switcher.log"
ROUTE_FILE="/etc/network/if-up.d/route"
# Check we're root
if [[ $EUID -ne 0 ]]; then
err_msg="This script must be run as root"
echo $err_msg | tee $LOG_FILE
exit 1
fi
###############################################################################
#
# Activate wiperf:
#
# 1. Check that a mode is defined in config.ini
# 2. Check interface wlan0 is available (wireless NIC plugged in)
# a. wlan0 - wireless NIC available
# b. eth0 - ethernet port available
# 3. Read in test interval & offset from config file
# 4. Add crontab entry to perform 5 min polling
# 5. Add fw rules to harden eth0 & wlan0
# 6. Backup various existing files to allow restoration when wiperf
# deactivated
# 7. Move a number of existing files that need to be replaced
# 8. Create status file to indicate wiperf is active
# 9. Reboot the wlanpi to ensure clean activation
#
# Note: /etc/sysctl.conf included to ensure forwarding
# cannot be accidentally be enabled in probe mode if
# has been changed by some other process
#
###############################################################################
wiperf_on () {
echo "Starting switch from classic mode to wiperf mode" | tee $LOG_FILE
# check what state the WLAN Pi is in classic mode
PI_STATUS=`cat $STATUS_FILE | grep 'classic'` || true
if [ -z "$PI_STATUS" ]; then
echo "Failed - WLAN Pi is not in classic mode. (exiting)" | tee -a $LOG_FILE
exit 1
fi
############################################
# 1. Get the probe mode (ethernet/wireless)
############################################
MODE=`cat ${CFG_FILE} | grep 'probe_mode:' | awk -F'[: ]*' '{print $2}'` || true
if [ -z "$MODE" ]; then
echo "No mode found in config file (exiting)" | tee -a $LOG_FILE
exit 1
else
echo "Selected mode in cfg file: ${MODE}" | tee -a $LOG_FILE
fi
#######################################################################
# 2. Check if wlan or eth interface is available (depending on config)
#######################################################################
echo "Checking expected interfaces are available..." | tee -a $LOG_FILE
if [ "$MODE" = "ethernet" ]; then
# Check if eth0 available before we start changing things
TESTING_IF=`ip a | grep eth0` || true
if [ -z "$TESTING_IF" ]; then
echo "No Eth Interface available! (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Ethernet details: ${TESTING_IF}" | tee -a $LOG_FILE
else
# Check if wlan0 available before we start changing things
TESTING_IF=`ip a | grep wlan0` || true
if [ -z "$TESTING_IF" ]; then
echo "No WLAN Interface available! (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "WLAN details ${TESTING_IF}" | tee -a $LOG_FILE
fi
#######################################
# 3. Get test interval & offset value
#######################################
TEST_INTERVAL=`cat ${CFG_FILE} | grep 'test_interval:' | awk -F'[: ]*' '{print $2}'` || true
if [ -z "TEST_INTERVAL" ]; then
echo "No test interval in config file (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Test interval from config file: ${TEST_INTERVAL}" | tee -a $LOG_FILE
# Get test offset value
TEST_OFFSET=`cat ${CFG_FILE} | grep 'test_offset:' | awk -F'[: ]*' '{print $2}'` || true
if [ -z "TEST_OFFSET" ]; then
echo "No test offset in config file (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Test offset from config file: ${TEST_OFFSET}" | tee -a $LOG_FILE
########################
# 4. Add crontab entry
########################
# add crontab if does not exist already
if ! [ -e /var/spool/cron/crontabs/$USERNAME ]; then
echo "# " | crontab -u $USERNAME -
fi
line="${TEST_OFFSET}-59/${TEST_INTERVAL} * * * * /usr/bin/python3 ${HOME_DIR}/wiperf_run.py > /var/log/wiperf_cron.log 2>&1"
(crontab -u $USERNAME -l; echo "$line" ) | crontab -u $USERNAME -
LAST_RESULT=$?
if [ $LAST_RESULT -ne 0 ]; then
echo "Cron add failed" | tee -a $LOG_FILE
exit 0
fi
echo "Cron command added: ${line}" | tee -a $LOG_FILE
#################################################################################
# 5. Harden eth0 & wlan0 by adding fw rules - allow only ssh in on eth0 & wlan0
#################################################################################
ufw insert 1 allow in on eth0 to any port ssh
ufw insert 2 allow in on eth0 to any port http
ufw insert 3 deny in on eth0
ufw insert 4 allow in on wlan0 to any port ssh
ufw insert 5 allow in on wlan0 to any port http
ufw insert 6 deny in on wlan0
##################################
# 6. Backup existing config files
##################################
declare -a arr=(
"/etc/network/interfaces"
"/etc/sysctl.conf"
"/etc/wpa_supplicant/wpa_supplicant.conf"
)
echo "Backing up config files..." | tee -a $LOG_FILE
for item in "${arr[@]}"
do
if [ -f "$item" ]; then
mv $item ${item}.probe
fi
done
##################################
# 7. Link to wiperf config files
##################################
echo "Linking temp config files..." | tee -a $LOG_FILE
ln -s ${CFG_DIR}/conf/etc/network/interfaces /etc/network/interfaces
ln -s ${CFG_DIR}/conf/etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
ln -s ${CFG_DIR}/conf/etc/sysctl.conf /etc/sysctl.conf
############################################################
# 8. Create status file to display status messages on FPMS
############################################################
echo "Updating status file." | tee -a $LOG_FILE
# Signal that wconsole active
echo "wiperf" > $STATUS_FILE
#############
# 9. Reboot
#############
echo "WLAN Pi will now reboot" | tee -a $LOG_FILE
sleep 1
sync
reboot
}
###############################################################################
#
# Deactivate wiperf:
#
# 1. Get the probe mode
# 2. Remove crontab entry to stop polling
# 3. Remove fw rules added during mode switch
# 4. Remove links created during activation
# 5. Restore config files backed up during activation
# 6. Remove status file to indicate wiperf no longer active
# 7. Reboot wlanpi to provide clean restoration of services
#
###############################################################################
wiperf_off () {
# check what state the WLAN Pi is in
PI_STATUS=`cat $STATUS_FILE | grep 'wiperf'` || true
if [ -z "$PI_STATUS" ]; then
echo "Failed - WLAN Pi is not in wiperf mode. (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Starting switch from wiperf mode to classic mode" | tee -a $LOG_FILE
############################################
# 1. Get the probe mode (ethernet/wireless)
############################################
MODE=`cat ${CFG_FILE} | grep 'probe_mode:' | awk -F'[: ]*' '{print $2}'` || true
if [ -z "$MODE" ]; then
echo "No mode found in config file (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Selected mode in cfg file: ${MODE}" | tee -a $LOG_FILE
###########################################
# 2. Remove crontab entry to stop polling
###########################################
echo "Removing cron job." | tee -a $LOG_FILE
crontab -u $USERNAME -l | grep -v 'wiperf_run.py' | crontab -u $USERNAME -
LAST_RESULT=$?
if [ $LAST_RESULT -ne 0 ]; then
echo "Cron remove failed (exiting)" | tee -a $LOG_FILE
exit 1
fi
echo "Cron entry removed" | tee -a $LOG_FILE
##################################
# 3. Remove eth0 & wlan0 fw rules
##################################
ufw delete allow in on eth0 to any port ssh
ufw delete allow in on eth0 to any port http
ufw delete deny in on eth0
ufw delete allow in on wlan0 to any port ssh
ufw delete allow in on wlan0 to any port http
ufw delete deny in on wlan0
##################################
# 4. Remove links to config files
##################################
echo "Removing links to temp config files" | tee -a $LOG_FILE
unlink /etc/network/interfaces
unlink /etc/sysctl.conf
unlink /etc/wpa_supplicant/wpa_supplicant.conf
###################################
# 5. Restore original config files
###################################
declare -a arr=(
"/etc/network/interfaces"
"/etc/sysctl.conf"
"/etc/wpa_supplicant/wpa_supplicant.conf"
)
echo "Restoring original config files" | tee -a $LOG_FILE
for item in "${arr[@]}"
do
if [ -f "${item}.probe" ]; then
mv ${item}.probe $item
fi
done
###################################
# 6. Update FPMS status file
###################################
echo "WLANPi will now reboot"
echo "classic" > $STATUS_FILE
###################################
# 7. Reboot
###################################
echo "Rebooting" | tee -a $LOG_FILE
sleep 1
sync
reboot
}
status () {
PI_STATUS=`cat $STATUS_FILE | grep 'wiperf'` || true
if [ -z "$PI_STATUS" ]; then
echo "wiperf is currently disabled"
exit 0
else
echo "wiperf is currently enabled"
exit 0
fi
}
case "$1" in
on)
wiperf_on
;;
off)
wiperf_off
;;
status)
status
;;
*)
N=/usr/share/wiperf/$NAME
echo "Usage: $N {on|off|status|version}" >&2
exit 0
;;
esac
exit 0