Created
December 19, 2022 13:36
-
-
Save ivanstepanovftw/27376da49cc276b6b6638e7c3bddbb51 to your computer and use it in GitHub Desktop.
Is WiFi or internet is down?
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
#/usr/bin/bash | |
ping_tolerance=1 | |
ok_retry_time=1 | |
fail_retry_time=1 | |
log() { | |
echo "$(date) - $@" | |
} | |
check_internet() { | |
local route="$(ip route)" | |
[[ -z "$route" ]] && echo "No route found" && return 1 | |
local default_gateway=$(echo "$route" | grep default | awk '{print $3}') | |
[[ -z "$default_gateway" ]] && echo "No default gateway found" && return 2 | |
local ping_gateway=$(ping -c1 -w$ping_tolerance $default_gateway | grep "1 received") | |
[[ -z "$ping_gateway" ]] && echo "Failed to ping default gateway" && return 3 | |
local ping_google=$(ping -c1 -w$ping_tolerance google.com | grep "1 received") | |
[[ -z "$ping_google" ]] && echo "Failed to ping google.com" && return 4 | |
return 0 | |
} | |
# Main loop | |
while true; do | |
internet_status=$(check_internet) | |
if [[ $? == 0 ]]; then | |
log "Internet is up" | |
# Some optimizations | |
while [[ -n "$(ping -c1 -w$ping_tolerance google.com | grep "1 received")" ]]; do | |
sleep $ok_retry_time | |
done | |
else | |
log "Internet is down: $internet_status" | |
echo -n $'\a' | |
sleep $fail_retry_time | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ensure you are connected to your router
Ensure you are connected to your router
Check your router
Provider issue