12

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
  • 21
  • 30
  • 30
Suezy
  • 223
  • 1
  • 2
  • 5

8 Answers8

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

This should solve your problem.

Alex
  • 3,079
  • 20
  • 28
David Pashley
  • 23,151
  • 2
  • 41
  • 71
  • @Suezy Ha! See? awk! – Jeremy Powell Jul 30 '09 at 07:54
  • close single-quote / close bracket mismatch on the end I think - works great otherwise though! – Mark K Cowan Aug 21 '13 at 20:01
  • 3
    I've seen quite a few answers online with this command, but non of them explain why "show default" is used. In fact "ip route show" without any parameter does exactly the same thing for me. So my question is why has "default" in the command? What does it mean? – user658991 Apr 18 '17 at 09:38
  • @user658991 I've been down this rabbit hole and you're exactly right. It does *nothing* on the most common versions of `ip` out there. In fact `ip route show` is an alias for `ip route list` and you can check `man ip` to see that this subcommand has the syntax `ip route { list | flush } SELECTOR`... And not a single one of those valid selectors listed is `default`. I feel like `ip route show table default` **should** do what we want, but unfortunately, it does not. To anybody else, match the beginning of the line, too (you've been warned): `ip route show | awk '/^default/{print $3}'` – L0j1k Aug 23 '18 at 13:25
6
 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
  • +1 for demonstrating direct native filtering via [iproute2](http://www.policyrouting.org/iproute2.doc.html)! there are so many cases where this amazing utility suite is more accurate, reliable, and performant than (admittedly creative) legacy Linux textfu – RubyTuesdayDONO Mar 09 '16 at 15:39
  • 1
    This is a great answer! but i am still a bit confused by iproute2's documentation. What does the "default" in the command signify? If i am reading the doc correctly, this command should be the same as "ip route show to default 0/0", am I right? what is the "0/0" format? – user658991 Apr 18 '17 at 09:40
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,041
  • 20
  • 28
  • i tried ip route and it displays the list for all the gateways.. and yes, the default one is also displayed on the bottom. thanks for that. =) But my goal here is to get the IP address used for that default gateway. Using bash script, I will ping that one to check if its up or not. – Suezy Jul 30 '09 at 07:05
  • I get four entries for "show default". "ip route show default | awk '/default/ {print $3}'" should get you the IP address alone. – David Pashley Jul 30 '09 at 07:06
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
  • 79,345
  • 17
  • 128
  • 213
  • 1
    `grep -e` or `egrep` would also do the trick without needing the brackets and the pipe to be escaped `egrep '^(default|0\.0\.0\.0)'` – Mark K Cowan Aug 21 '13 at 20:04
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.

user155186
  • 21
  • 1
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

Lucas Kauffman
  • 16,818
  • 9
  • 57
  • 92
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