Sometimes it is necessary to check whether server you want to run some big bash script is connected to Internet. Usually it makes sense while running scripts periodically using cron. Below is the tiny bash script for this purpose:
#!/bin/bash WGET="/usr/bin/wget" $WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null if [ ! -s /tmp/index.google ];then echo "no" else echo "yes" fi
As you see it tries to download google’s index page, if it’s not empty script returns “yes”, if there is not Internet connection available script will return “no”. If it is impossible to fetch the page in more than 5 seconds script will return “no” as well.
Anything to add? You are welcome! 🙂
Whatever happened to good old ping and tracert? ping -c 4 208.67.222.222 –>> this sends 4 pings (to the opendns IP) ofc, you’ll want to check what other nifty little flags ping has. Careful, sending too many pings to an IP might throw some warning flags in your direction. Remember, be considerate.
That was very helpful! Thank You!
thanks a lot! very useful script 😀