Last active
October 2, 2023 00:14
-
-
Save bmatthewshea/87b70e3d75f8f8576cacdf1445daf4a6 to your computer and use it in GitHub Desktop.
When Do I Go Back Online? (Capture a combined ping/grep reply in a variable)
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 | |
# WHEN DO I GO BACK ONLINE? | |
# Brady M. Shea - 23SEP2023 | |
# For a StackOverflow answer (https://stackoverflow.com/a/77164885/503621) | |
# Original link: https://gist.github.com/bmatthewshea/87b70e3d75f8f8576cacdf1445daf4a6 | |
# Variables/command subs | |
sleepy=60 # PING every SLEEPY seconds. 1 minute recommended. | |
hosttoping=google.com # can be google.com/etc. Should be a name based host. Not an IP. | |
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
checkinternet() { | |
checkup=$(eval 'ping -4 -c 1 ${hosttoping} | grep "64 bytes"') | |
} | |
sleeptimer() { | |
for (( count=1; count<=$sleepy; count++ )); do | |
sleep 1; | |
done | |
printf "\n" | |
} | |
ts() { | |
timestamp=$(date +"%d %b %Y %H:%M:%S %Z") | |
} | |
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
ts && printf "\nStarted: ${timestamp}\n\n" | |
while | |
checkinternet | |
[[ "$checkup" == "" ]] | |
do | |
ts && printf "${timestamp} - Your ISP is (probably) down! No reply from google yet. Sleeping ${sleepy} seconds"; sleeptimer | |
done | |
printf "\n\nExiting.\n\n" | |
ts && printf "\n\nInternet is back up at: ${timestamp}\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment