One that checks if there is a default gateway and if so pings it and displays both the gateway and the response time:
Code:
shopt -s extglob
GW=$(netstat -rn|grep ^default|awk '{print $2}')
if [[ ! $GW == +([[:digit:]]).+([[:digit:]]).+([[:digit:]]).+([[:digit:]]) ]]; then
echo "no default gateway"
exit 1
fi
IFS=''
PING=`ping -c1 -n -t5 -o $GW`
RC=$?
if [ $RC -eq 2 ]; then
echo "gateway ($GW) no reply"
exit 1
fi
RT=$( echo $PING | head -n2 | tail -n1 | awk -F= '{print $4}' )
echo "gateway ($GW) $RT"
exit $RC
One that just checks a random IP on the internet to see if we have internet access:
Code:
shopt -s extglob
GW=216.239.32.10
IFS=''
PING=`ping -c1 -n -t5 -o $GW`
RC=$?
if [ $RC -eq 2 ]; then
echo "Internet ($GW) no reply"
exit 1
fi
RT=$( echo $PING | head -n2 | tail -n1 | awk -F= '{print $4}' )
echo "Internet ($GW) $RT"
exit $RC