Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Last active October 2, 2023 00:14
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 bmatthewshea/87b70e3d75f8f8576cacdf1445daf4a6 to your computer and use it in GitHub Desktop.
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)
#!/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