13

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.

8 Answers 8

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

This should solve your problem.

4
  • @Suezy Ha! See? awk! Jul 30, 2009 at 7:54
  • close single-quote / close bracket mismatch on the end I think - works great otherwise though! Aug 21, 2013 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, 2017 at 9: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, 2018 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
2
  • +1 for demonstrating direct native filtering via iproute2! there are so many cases where this amazing utility suite is more accurate, reliable, and performant than (admittedly creative) legacy Linux textfu Mar 9, 2016 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, 2017 at 9: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.

2
  • 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, 2009 at 7:05
  • I get four entries for "show default". "ip route show default | awk '/default/ {print $3}'" should get you the IP address alone. Jul 30, 2009 at 7: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)
1
  • 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)' Aug 21, 2013 at 20:04
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

1
  • Why grep default when you already specify the default route in your ip command?
    – lindhe
    Dec 29, 2019 at 14:43
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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .