How to do it...

  1. Open a terminal and create the test_ipv4.sh script:
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then  echo "IPv4 is up"else  echo "IPv4 is down"fi
  1. Now, to test IP connectivity and DNS, create a script called test_ip_dns.sh:
if ping -q -c 1 -W 1 google.com >/dev/nullthen  echo "The network is up"else  echo "The network is down"fi
  1. Lastly, create a script called test_web.sh to test web connectivity:
case "$(curl -s --max-time 2 -I http://google.com | sed 's/^[^ ]*  *\([0-9]\).*/\1/; 1q')" in  [23]) echo "HTTP connectivity is up";;  5) echo "The web proxy won't let us through";;  *) echo "The network is down or very slow";;esac

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.