-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebootrax75.sh
More file actions
executable file
·124 lines (108 loc) · 4.11 KB
/
rebootrax75.sh
File metadata and controls
executable file
·124 lines (108 loc) · 4.11 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
#!/bin/bash
# Name: rebootrax75.sh
# Date: 2025-11-21 (Refactored)
# Author: Chuck Renner
# License: MIT License
# Version 0.3.0
# --- CONFIG LOADING ---
# 1. Check for local config (dev/testing)
# 2. Check for installed config (production)
OVERRIDE_DRY_RUN=${DRY_RUN:-}
if [[ -f "./config.env" ]]; then
source "./config.env"
elif [[ -f "/etc/rebootrax75/config.env" ]]; then
source "/etc/rebootrax75/config.env"
else
echo "Error: Configuration file not found." >&2
exit 1
fi
if [[ -n "$OVERRIDE_DRY_RUN" ]]; then
DRY_RUN="$OVERRIDE_DRY_RUN"
fi
# ----------------------
# Exit if script is not running with root privileges.
if [ `id -u` -ne 0 ]; then
echo "Run this script using sudo!"
exit 1
fi
# Define functions
function str_random() {
array=()
for i in {a..z} {A..Z} {0..9}; do
array[$RANDOM]=$i
done
printf %s ${array[@]::8}
}
function urlencode() {
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:$i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
function urldecode() {
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
INSECURE_OPTION=
VERBOSE_OPTION=
if [ ${INSECURE:-0} -eq 1 ]; then INSECURE_OPTION=--insecure; fi
if [ ${VERBOSE:-0} -eq 1 ]; then VERBOSE_OPTION=--verbose; fi
# Temporary directory
DUMPDIR=/tmp
# WAP Fully Qualified Domain Name (FQDN)
WFQDN=$WHOST.$WDOM
# Cookie Jar file
JAR=$DUMPDIR/$WHOST.cjar
# Curl output files prefix
OUT=$DUMPDIR/$WHOST
# URL encode the printer username and password
WUSER_ENC=$(urlencode "$WUSER")
WPASS_ENC=$(urlencode "$WPASS")
echo Logging into WAP
if [ ${VERBOSE:-0} -eq 1 ]; then
echo Running curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u \"$WUSER_ENC:\<\$WPASS_ENC\>\" \"https://$WFQDN/\"
fi
curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u "$WUSER_ENC:$WPASS_ENC" "https://$WFQDN/" --output - 1>$OUT-1.html 2>$OUT-1.err
chmod 600 $JAR
chmod 600 $OUT-1.*
if [ ${VERBOSE:-0} -eq 1 ]; then
echo Running curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u \"$WUSER_ENC:\<\$WPASS_ENC\>\" \"https://$WFQDN/ADVANCED_home.htm\"
fi
curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u "$WUSER_ENC:$WPASS_ENC" "https://$WFQDN//ADVANCED_home.htm" --output - 1>$OUT-2.html 2>$OUT-2.err
chmod 600 $OUT-2.*
if [ ${VERBOSE:-0} -eq 1 ]; then
echo Running curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u \"$WUSER_ENC:\<\$WPASS_ENC\>\" --referer \"https://$WFQDN/ADVANCED_home.htm\" \"https://$WFQDN/ADVANCED_home1.htm\"
fi
curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u "$WUSER_ENC:$WPASS_ENC" --referer "https://$WFQDN/ADVANCED_home.htm" "https://$WFQDN/ADVANCED_home1.htm" --output - 1>$OUT-3.html 2>$OUT-3.err
chmod 600 $OUT-3.*
if [ ${VERBOSE:-0} -eq 1 ]; then
echo Getting form id from curl...
fi
FORM_ACTION=`curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u "$WUSER_ENC:$WPASS_ENC" --referer "https://$WFQDN/ADVANCED_home1.htm" "https://$WFQDN/ADVANCED_home2.htm" --output - 2>$OUT-4.err | grep newgui_adv_home.cgi | sed 's/^.*\(newgui_adv_home.cgi?id=[a-f0-9]\+\).*$/\1/'`
chmod 600 $OUT-4.*
if [ ${VERBOSE:-0} -eq 1 ]; then
echo Set FORM_ACTION to $FORM_ACTION
fi
echo Attempting reboot of WAP
if [ ${DRY_RUN:-0} -eq 1 ]; then
FORM_BUTTON=
else
FORM_BUTTON='--form "buttonSelect=2"'
fi
# The reboot command
curl $VERBOSE_OPTION $INSECURE_OPTION --cookie-jar $JAR --cookie $JAR -u "$WUSER_ENC:$WPASS_ENC" --referer "https://$WFQDN/ADVANCED_home2.htm" $FORM_BUTTON "https://$WFQDN/$FORM_ACTION" --output - 1>$OUT-Final.html 2>$OUT-Final.err
chmod 600 $OUT-Final.*
echo "WAP should be rebooting; WLAN should be offline for about 2 minutes"
# Cleanup
rm -f $JAR $OUT-?.*
echo "Results highlights from WAP reboot information page:"
grep 'The router is rebooting' $OUT-Final.html || echo "Reboot confirmation not found (check logs)."
rm -f $OUT-Final.*
echo "Completed WAP reboot script"