Posted By

sudarkoff on 08/28/06


Tagged

Bash network geektool internet status


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

yuconner


Find and ping default gateway


 / Published in: Other
 

URL: http://projects.tynsoe.org/phpBB2/viewtopic.php?p=745

  1. One that checks if there is a default gateway and if so pings it and displays both the gateway and the response time:
  2. Code:
  3.  
  4. shopt -s extglob
  5. GW=$(netstat -rn|grep ^default|awk '{print $2}')
  6. if [[ ! $GW == +([[:digit:]]).+([[:digit:]]).+([[:digit:]]).+([[:digit:]]) ]]; then
  7. echo "no default gateway"
  8. exit 1
  9. fi
  10. IFS=''
  11. PING=`ping -c1 -n -t5 -o $GW`
  12. RC=$?
  13. if [ $RC -eq 2 ]; then
  14. echo "gateway ($GW) no reply"
  15. exit 1
  16. fi
  17. RT=$( echo $PING | head -n2 | tail -n1 | awk -F= '{print $4}' )
  18. echo "gateway ($GW) $RT"
  19. exit $RC

Report this snippet  

You need to login to post a comment.