Last active
May 15, 2023 16:51
-
-
Save kmahyyg/6899f2c1958457c3991f523347d9b456 to your computer and use it in GitHub Desktop.
OpenWRT router online check and re-up
This file contains hidden or 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 | |
# | |
# This script is running on OpenWRT 18.04.3 | |
# AArch64-Xiaomi-AX6S | |
# | |
# Requirements: | |
# bash 5.1.16 | |
# curl 7.83.1 (2022-05-11) | |
# | |
# This script should be run with an interval longer than LCP request. | |
# Currently, the LCP request and response threshold is using a time window of 100 seconds, so this script should be run per 2 minutes. | |
# | |
# Aiming at assist for online detection, if LCP does NOT detect PPP session terminated | |
# after 2 retries with 5 seconds interval, this script will bring down WAN and bring it up again. | |
# after bring it down and up, wait for 15 seconds, if still not online and RESERVE_VOLATILE_LOG == 0, reboot the whole router | |
# | |
# Currently, the PPP session will be restarted if 20 failures of LCP request, each request has an interval of 5 seconds. | |
# | |
# Crontab: | |
# */5 * * * * /bin/bash /etc/router-onlinecheck.sh | |
IFUP_BIN="/sbin/ifup" | |
IFDOWN_BIN="/sbin/ifdown" | |
RBT_BIN="/sbin/reboot" | |
WAN_IFACE="wan wan6" | |
RESERVE_VOLATILE_LOG=1 | |
LOG_BIN="/usr/bin/logger" | |
PROGNAME="yconncheck" | |
CURL_BIN="/usr/bin/curl" | |
BARK_APIURL="https://bark.pushserver.example.com/push" | |
BARK_CRED="myuser:mypassword" | |
BARK_DEST_DEV="my-mobile-dev-push-device-id" | |
NET_LOC="my-home-network-location" | |
function ReOnlinePush(){ | |
${CURL_BIN} -X "POST" "${BARK_APIURL}" \ | |
-H 'Content-Type: application/json; charset=utf-8' \ | |
-u "${BARK_CRED}" \ | |
-d "{\"badge\":1,\"device_key\":\"${BARK_DEST_DEV}\",\"isArchive\":\"1\",\"title\":\"Connectivity Health\",\"level\":\"timeSensitive\",\"group\":\"NetworkAvail\",\"body\": \"Network is restarted on ${NET_LOC}\"}" \ | |
-m 3 --connect-time 3 | |
${LOG_BIN} -t "${PROGNAME}" "Send a push notification to admin via Bark." | |
} | |
CON_ONLINE=$(${CURL_BIN} --connect-time 3 -m 3 -s --retry-delay 5 --retry 2 --retry-all-errors -S -o /dev/null -w '%{http_code}' "https://connect.rom.miui.com/generate_204") | |
if [[ ${CON_ONLINE} -eq 204 ]]; then | |
${LOG_BIN} -t "${PROGNAME}" "Network connection is online." | |
exit 0 | |
else | |
${LOG_BIN} -t "${PROGNAME}" "Connection Offline." | |
${IFDOWN_BIN} ${WAN_IFACE} | |
sleep 5 | |
${IFUP_BIN} ${WAN_IFACE} | |
sleep 15 | |
AGN_CON_ONLINE=$(${CURL_BIN} --connect-time 3 -m 3 -s --retry-delay 5 --retry 2 --retry-all-errors -S -o /dev/null -w '%{http_code}' "https://connect.rom.miui.com/generate_204") | |
if [[ ${AGN_CON_ONLINE} -eq 204 ]]; then | |
${LOG_BIN} -t "${PROGNAME}" "Connection online after bring down and up." | |
ReOnlinePush | |
exit 0 | |
else | |
${LOG_BIN} -t "${PROGNAME}" "Connection cannot be automatically recovered from disaster." | |
if [[ ${RESERVE_VOLATILE_LOG} -eq 0 ]]; then | |
${LOG_BIN} -t "${PROGNAME}" "Configured to reboot after 15s." | |
sleep 15 | |
${RBT_BIN} | |
exit 0 | |
fi | |
exit 0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment