Last active
September 28, 2021 00:13
-
-
Save shirish87/c6e48b08616354d94d9147a4acfe7655 to your computer and use it in GitHub Desktop.
Script on the intranet raspberry pi to reconnect the PPPoE internet connection in the TP-Link gateway. Notifies other devices using push notifs via Gotify server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
###### | |
# Installed under cron for root user | |
# sudo crontab -e | |
# * * * * * bash /home/marvin/cron-scripts/check-inet.sh >> /tmp/check-inet.log 2>&1 | |
###### | |
GATEWAY_IP=192.168.0.1 | |
GATEWAY_AUTH="Basic <base64:userpass>" | |
AGENT=" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" | |
check () { | |
local STATE="error" | |
for i in $(seq 1 $2); do | |
STATE=$(ping -q -w 1 -c 1 "$1" > /dev/null && echo ok || echo error) | |
[[ "$STATE" == "ok" ]] && echo "check:$1:connected:$1" && return 0 | |
sleep 1 | |
echo "check:$1:retry:$i" | |
done | |
return 1 | |
} | |
check_gateway () { | |
check `ip r | grep default | cut -d ' ' -f 3` 10 | |
STATE=$? | |
return $STATE | |
} | |
check_internet_dns () { | |
check 8.8.8.8 3 | |
STATE=$? | |
return $STATE | |
} | |
inet_disconn () { | |
echo "disconnecting..." | |
curl -s "http://$GATEWAY_IP/cgi?7" -H "Origin: http://$GATEWAY_IP" -H 'Content-Type: text/plain' -H 'Accept: */*' -H "Cookie: Authorization=$GATEWAY_AUTH" --data-binary $'[ACT_PPP_DISCONN#1,1,1,0,0,0#0,0,0,0,0,0]0,0\r\n' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: text/plain' -H 'Accept: */*' -H "Referer: http://$GATEWAY_IP/mainFrame.htm" -H 'Accept-Language: en-US,en;q=0.9' -H "User-Agent: $AGENT" --compressed --insecure > /dev/null | |
} | |
inet_conn () { | |
echo "connecting..." | |
curl -s "http://$GATEWAY_IP/cgi?7" -H "Origin: http://$GATEWAY_IP" -H 'Content-Type: text/plain' -H 'Accept: */*' -H "Cookie: Authorization=$GATEWAY_AUTH" --data-binary $'[ACT_PPP_CONN#1,1,1,0,0,0#0,0,0,0,0,0]0,0\r\n' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: text/plain' -H 'Accept: */*' -H "Referer: http://$GATEWAY_IP/mainFrame.htm" -H 'Accept-Language: en-US,en;q=0.9' -H "User-Agent: $AGENT" --compressed --insecure > /dev/null | |
} | |
inet_reconn () { | |
inet_disconn | |
sleep 2 | |
inet_conn | |
} | |
check_gateway | |
GATEWAY_STATE=$? | |
if [[ $GATEWAY_STATE -eq 0 ]]; then | |
check_internet_dns | |
NET_STATE=$? | |
if [[ $NET_STATE -eq 1 ]]; then | |
was_down=1 | |
while [[ $was_down -eq 1 ]]; do | |
inet_reconn | |
sleep 5 | |
check_internet_dns | |
NET_STATE=$? | |
if [[ $NET_STATE -eq 0 ]]; then | |
echo "NOW CONNECTED!" | |
was_down=0 | |
/usr/local/bin/gotify push -t "Home Internet Status" -p 10 "ONLINE" | |
break | |
fi | |
done | |
else | |
echo "NET IS UP!" | |
fi | |
else | |
echo "GATEWAY DONW!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment