Is there an easy way to check Internet connectivity from console? I am trying to play around in a shell script.
One idea I seem is to
wget --spider http://www.google.co.in/ and check the HTTP response code to interpret if the Internet connection is working fine. But I think there must be easy way without the need of checking a site that never crash ;)
Edit: Seems like there can be a lot of factors which can be individually examined, good thing. My intention at the moment is to check if my blog is down. I have setup cron to check it every minute. For this, I am checking the HTTP response code of wget --spider to my blog. If its not 200, it notifies me (I believe this will be better than just pinging it, as the site may under be heavy load and may be timing out or respond very late). Now yesterday, there was some problem with my Internet. LAN was connected fine but just I couldn't access any site. So I keep on getting notifications as the script couldn't find 200 in the wget response. Now I want to make sure that it displays me notification when I do have internet connectivity.
So, checking for DNS and LAN connectivity is a bit overkill for me as I don't have that much specific need to figure out what problem it is. So what do you suggest how I do it?
Here is my script to keep checking downtime for my blog:
#!/bin/bash # Sending the output of the wget in a variable and not what wget fetches RESULT=`wget --spider http://blog.ashfame.com 2>&1` FLAG=0 # Traverse the string considering it as an array of words for x in $RESULT; do if [ "$x" = '200' ]; then FLAG=1 # This means all good fi done if [ $FLAG -eq '0' ]; then # A good point is to check if the internet is working or not # Check if we have internet connectivity by some other site RESULT=`wget --spider http://www.facebook.com 2>&1` for x in $RESULT; do if [ "$x" = '200' ]; then FLAG=1 # This means we do have internet connectivity and the blog is actually down fi done if [ $FLAG -eq '1' ]; then DISPLAY=:0 notify-send -t 2000 -i /home/ashfame/Dropbox/Ubuntu/icons/network-idle.png "Downtime Alert!" "http://blog.ashfame.com/ is down." fi fi exit
This way I need to check for internet connectivity only where there is an issue with my blog response code. Its a bit heavy (as I am not using ping) but should not give any false positives. Right? Also how can I randomize pinging to a different site everytime, like facebook, google, yahoo etc. Also (I was trying to avoid any I/O) I can write to a log file by which I can check the count of downtime checks and then skip further checks till the site is down or cause longer checks (10mins instead of every min). What do you think?