-
Notifications
You must be signed in to change notification settings - Fork 66
/
add_route.sh
executable file
·121 lines (112 loc) · 3.78 KB
/
add_route.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
#!/bin/bash
#
# add_route.sh
# Script to setup persistant TEDAPI Routing for Powerwall Dashboard
# Version: 1.0.1
# By Scott Hibbard - 2024-09-15
#
CRONTAB="/var/spool/cron/crontabs/root"
PW_IP=""
SCRIPT_NAME="TEDAPI_routing"
DIR="/root/scripts"
LINUX_IP="192.168.91.0/24"
TARGET_IP="192.168.91.1"
NETMASK="255.255.255.255"
OS=$(uname -s)
echo "Setup script for persistant Powerall Dashboard TEDAPI Interface network routing"
echo "-------------------------------------------------------------------------------"
echo
echo "This script will require root privileges to read & set a startup cron task."
read -r -p "Do you want to run this script? [Y/n] " response
if [[ "$response" =~ ^([nN][oO]|[nN])$ ]]; then
echo "Cancel"
exit 1
fi
while [ "$PW_IP" == "" ]; do
read -p 'Enter Powerwall IP Address: ' PW_IP
done
# Detect OS and run commands accordingly
if [[ "${OS}" == "Linux" ]]; then
# Check if running under WSL
if grep -qi "microsoft" /proc/version; then
echo "WSL detected - unable to add route automatically."
echo "To add the route, open an Administrator Shell in Windows and run:"
echo " route -p add ${TARGET_IP} mask ${NETMASK} ${PW_IP}"
echo ""
read -p "Press Enter to exit..."
exit 1
else
echo "Native Linux detected"
if $(ip route | grep -qw ${LINUX_IP}); then
read -r -p "${LINUX_IP} routing already in routing table. Still want to run this? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Cancel"
exit 1
fi
fi
sudo mkdir -p ${DIR}
if [ -f ${DIR}/${SCRIPT_NAME}.sh ]; then
echo "Boot script already exists (${DIR}/${SCRIPT_NAME}.sh)."
else
echo ""
cat > ${SCRIPT_NAME}.tmp << EOF
#!/bin/bash
#
# Add network route to Powerwall TEDAPI address on boot.
# This script is auto-generated by Powerwall Dashboard add_route.sh
#
declare -i i=0
while (( i < 15 )); do
RETURN="\$(ip route add ${LINUX_IP} via ${PW_IP} 2>&1)"
if [ "\$RETURN" != "RTNETLINK answers: File exists" ]; then
declare -i i=i+1
sleep 1
else
RETURN="Success"
break
fi
done
# Uncomment the lines below to log results of executing this script
# NOW="\$(date)"
# echo "\${NOW}: result=\${RETURN}, delay=\${i}" >> ${DIR}/${SCRIPT_NAME}.log
EOF
chmod 775 ${SCRIPT_NAME}.tmp
sudo chown 0 ${SCRIPT_NAME}.tmp
sudo mv ${SCRIPT_NAME}.tmp ${DIR}/${SCRIPT_NAME}.sh
fi
if ! (sudo test -f ${CRONTAB}) || ! (sudo grep -qw "${DIR}/${SCRIPT_NAME}.sh" ${CRONTAB}); then
(sudo crontab -u root -l 2>/dev/null; echo "@reboot ${DIR}/${SCRIPT_NAME}.sh") | sudo crontab -u root -
echo "Cron entry added."
else
echo "Cron line already exists."
fi
sudo /bin/bash ${DIR}/${SCRIPT_NAME}.sh
echo "Installation complete."
exit 0
fi
elif [[ "${OS}" == "Darwin" ]]; then
echo "macOS detected - adding permanent route for Wi-Fi" # TODO: Support for other network interfaces (Ethernet, etc.)
if sudo networksetup -setadditionalroutes Wi-Fi "${TARGET_IP}" "${NETMASK}" "${PW_IP}"; then
echo "Route added successfully."
else
echo "Failed to add the route. Please check your network configuration or permissions."
exit 1
fi
echo ""
exit 0
elif [[ "${OS}" =~ MINGW* || "${OS}" =~ CYGWIN* ]]; then
echo "Windows shell detected - attempting to add route automatically."
if route -p add "${TARGET_IP}" mask "${NETMASK}" "${PW_IP}"; then
echo "Route added successfully."
else
echo "Failed to add the route. Please ensure you are running as Administrator."
read -p "Press Enter to exit..."
exit 1
fi
echo ""
exit 0
else
echo "You are running '$OS', which is not supported in this script."
echo "Maybe you could add code to support '$OS'!"
exit 1
fi