Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

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

Should do what you want.

share|improve this answer
    
thanks a lot..it works. =) –  Suezy Jul 30 '09 at 7:38
    
@Suezy Ha! See? awk! –  Jeremy Powell Jul 30 '09 at 7:54
    
lol..yea. =) thanks too. –  Suezy Jul 30 '09 at 7:57
    
close single-quote / close bracket mismatch on the end I think - works great otherwise though! –  Mark K Cowan Aug 21 '13 at 20:01

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.

share|improve this answer
    
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 7: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 7:06

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}
share|improve this answer
 ip route show default | grep default | awk {'print $3'}

Does it here

share|improve this answer

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)
share|improve this answer
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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.