Created
September 20, 2021 13:17
-
-
Save JaxxArmstrong/f3dfc7e53026a0d249c06fc5bf7036e7 to your computer and use it in GitHub Desktop.
Online and RTT check | Bash
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 | |
# | |
# Internet connection check and take note | |
# of the roundtrip time to specified host. | |
# | |
# For use on a panel. | |
# | |
HOST="4.2.2.2" | |
TODAY=$(date +%Y-%m-%d/%H:%M:%S) | |
LOGFILE="$HOME/.internet_connectivity.log" | |
function _exit () { | |
echo "Online check disabled" > $HOME/.online_check | |
} | |
# Execute at script end. | |
trap "_exit" 0 | |
# Main | |
while [ 1 ] | |
do | |
rtt=$(ping $HOST -w 3 -c 1 | sed -rn 's/.*time=([0-9]{1,})\.?[0-9]{0,} ms.*/\1/p') | |
onlineStatus=$(ping -c 1 -q google.com >&/dev/null; echo $?) | |
if [ "$onlineStatus" -eq 0 ]; then | |
echo "Online ($rtt ms)" > $HOME/.online_check | |
# echo "$TODAY Online" >> "$LOGFILE" | |
else | |
echo "Offline" > $HOME/.online_check | |
echo "$TODAY Offline" >> "$LOGFILE" | |
fi | |
sleep 5s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment