Last active
July 20, 2021 10:43
-
-
Save kfatehi/9cc5661780081f487850 to your computer and use it in GitHub Desktop.
Bash script that will block until internet access is available and return appropriate exit value
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 | |
# Blocks until there is internet access | |
# Accepts optional parameters: host and retry limit | |
# Return non-zero if we try to exceed the retry limit | |
# Return zero if we have internet within the retry limit | |
# Usage: /pingblock.sh google.com 30 | |
host=${1:-8.8.8.8} | |
max_tries=${2:-15} | |
num=0 | |
until ping -i2 -c1 $host > /dev/null; do | |
sleep 1 | |
echo "Checking for internet access... ($(($num + 1))/$max_tries)" | |
num=$(($num + 1)) | |
if [[ $num -eq $max_tries ]]; then | |
echo "There is no internet access!" | |
exit 1 | |
fi | |
done | |
echo "There is internet access!" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment