Find and ping default gateway


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.