Skip to content

Instantly share code, notes, and snippets.

@Ansen
Last active February 19, 2024 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ansen/4cad257ac3bff6f426d67f8c15a96b6a to your computer and use it in GitHub Desktop.
Save Ansen/4cad257ac3bff6f426d67f8c15a96b6a to your computer and use it in GitHub Desktop.
Automatically restart the device when the network is unavailabled
#!/bin/bash
. /etc/profile > /dev/null 2>&1
# PUB_DNS='1.1.1.1'
# CHINA ONLY
PUB_DNS='223.5.5.5'
# percentage
REBOOT_THRESHOLD=80
MAXIMUM_TEST_COUNT=10
# second
TEST_INTERVAL=10
PING_COUNT=5
function show_info(){
echo "TEST DNS: ${PUB_DNS}"
echo "TEST INTERVAL: ${TEST_INTERVAL}"
echo "TEST COUNT: ${MAXIMUM_TEST_COUNT}"
echo "Ping Count: ${PING_COUNT}"
}
function main(){
show_info
unavailable_count=0
for x in $(seq 1 ${MAXIMUM_TEST_COUNT})
do
ping -c ${PING_COUNT} ${PUB_DNS} &> /dev/null
if [[ $? -ne 0 ]]
then
echo "The network may be unavailable. Test: ${x}"
((unavailable_count++))
else
echo "The network is available. Test: ${x}"
fi
sleep ${TEST_INTERVAL}s
done
unavailable_threshold=$(awk 'BEGIN{printf "%d\n",('${unavailable_count}'/'${MAXIMUM_TEST_COUNT}')*100}')
if [[ ${unavailable_threshold} -ge ${REBOOT_THRESHOLD} ]]
then
echo "The network is unavailable and the device will be reboot."
reboot
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment