How do i get the default gateway in LINUX given the destination?

DEFAULT_ROUTE=$(ip route show default | awk '/default/ {print $3}')
ping -c 1 $DEFAULT_ROUTE

This should solve your problem.


 ip route show default 0.0.0.0/0

This command is much more quicker then ip route | grep default on system with many (10000+) routes

Look for example from my router with full BGP and manual default route as backup

router:~# time ip route show default 0.0.0.0/0
default via XXX.XXX.192.254 dev eth0.123 

real    0m0.707s
router:~# time ip route | grep default
default via XXX.XXX.192.254 dev eth0.123 

real    0m8.596s
  • because ip route show default show all routes for me not only default route
  • because ip route get not accept default or 0.0.0.0/0 as parameter