15

I'm trying to get the default gateway, using the destination 0.0.0.0

i used this command: netstat -rn | grep 0.0.0.0

and it returns this list:

Destination - Gateway      - Genmask         - Flags - MSS - Window - irtt - Iface
10.9.9.17   - 0.0.0.0      - 255.255.255.255 - UH    - 0     0        0    - tun0
133.88.0.0  - 0.0.0.0      - 255.255.0.0     - U     - 0     0        0    - eth0
0.0.0.0     - 133.88.31.70 - 0.0.0.0         - UG    - 0     0        0    - eth0

My goal here is to ping the default gateway using destination 0.0.0.0; thus, that is "133.88.31.70"; but this one returns a list because of using 'grep'.

Question is: How do i get the default gateway only? I will need it for my bash script to identify if net connection is up or not.

Dave M
  • 4,494
Suezy
  • 253

8 Answers8

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

This should solve your problem.

Alex
  • 3,129
  • 23
  • 28
David Pashley
  • 23,963
7
 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
3

ip route show default should do the trick for the default gateway, or you can use ip route get <someip> to see what the route for a particular IP address is.

Zanchey
  • 3,051
2

If you want to do this using netstat (so it'll work on something that's NOT Linux) the general solution is:

netstat -rn | grep '^\(default\|0\.0\.0\.0\)' | awk '{print $2}'

This shows the routing tables, using IPs rather than resolving hostnames (netstat -rn),
looks for the default gateway (lines beginning with 0.0.0.0 or default),
and prints the gateway host (second field of the results).

The proliferation of \ characters in the regular expression are significant:

  • \( and \) specify a group
  • \| is the alternation character (the thing on the left or the thing on the right matches)
  • the dots in the IP address are represented by \. - meaning the literal character . as opposed to a Regular Expression . (matching any single character)
voretaq7
  • 80,749
1
ip route show 0/0

or

ip route show 0.0.0.0/0

or

ip route show 0.0.0.0/0.0.0.0

or

ip -f inet route show default

or

ip -4 route show default

If I understand it correctly, the default gateway lookup for that exact route - the all-zeros route - is either 0.0.0.0/0.0.0.0 (or 0/0) for IPv4 or 0000:0000:0000:0000:0000:0000:0000:0000 (or ::) for IPv6.

However(!), without either specifying the AF you wish to use (-4 or -6 or -f inet or -f inet6), or specifying the all-zeros route as a IPv4 or IPv6 prefix (0/0 implies IPv4), it cannot figure out what 'default' is...

Personally, I don't feel the man page is very clear about this, but that's just my two cents.

1

This is how I do it:

#!/bin/sh
GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p")
echo ${GATEWAY_DEFAULT}
1
 ip route show default | grep default | awk {'print $3'}

Does it here

0

The VERY easy way... ask the routing table how it gets to an IP outside of it's network

Google DNS == 8.8.8.8

ip route get 8.8.8.8

Dave.B
  • 11