What command can you use to find the Gateway IP Address (ie. home router address) for eth0 in Linux?

I need to get the IP address from a command line app to use in a shell script.

link|improve this question
feedback

10 Answers

up vote 5 down vote accepted

To print out only the default gw IP:

route -n | grep 'UG[ \t]' | awk '{print $2}'

To print out route information on all interfaces:

route -n

or

route -rn
link|improve this answer
Wouldn't UG require the interface to be up? I think just G would be better. – J. Polfer Jun 24 '09 at 18:39
Either way, this is spot on. Many Thanx. – J. Polfer Jun 24 '09 at 18:40
Wanted to let you know - I'm actually using this answer, but with just a G in the grep pattern; didn't have iptools on the machine i need to run this on. – J. Polfer Jun 24 '09 at 21:09
1  
You can save yourself the grep, awk can also filter: route -n | awk '{if($4=="UG")print $2}' – Kenny Rasschaert Dec 20 '11 at 7:16
@KennyRasschaert yeap, that works! Thanks! – l0c0b0x Dec 20 '11 at 17:54
feedback

You can get the system's default gateway from the output of netstat -r or route

link|improve this answer
feedback
ip route show 0.0.0.0/0 dev eth0 | cut -d\  -f3

is my entry :)

link|improve this answer
I would improve this one: ip route show 0.0.0.0/0 | awk '{print $3}' – Dominic Eidson Jun 24 '09 at 19:38
I dunno... mine's still fewer keystrokes :) – MikeyB Jun 24 '09 at 20:57
feedback
$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.199.0   0.0.0.0         255.255.255.240 U         0 0          0 virbr1
192.168.200.0   0.0.0.0         255.255.255.240 U         0 0          0 virbr2
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
0.0.0.0         192.168.1.254   0.0.0.0         UG        0 0          0 wlan0

The 0.0.0.0 is your default gateway, pointing to 192.168.1.254 at my place.

link|improve this answer
feedback

The output from route -n or netstat -rn, and search for the destination 0.0.0.0.

link|improve this answer
feedback

I prefer the iproute package:

# get the default route
ip route list | awk ' /^default/ {print $3}'
# get the default route but limit on eth0 (output may be empty)
ip route list dev eth0 | awk ' /^default/ {print $3}'
link|improve this answer
@sheepsimulator: Unfortunately, this is a bad assumption to make. Supermathie's suggestion is better. – Dominic Eidson Jun 24 '09 at 19:34
@Dominic Eidson - Why? There's only one physical ethernet interface on the machine. – J. Polfer Jun 24 '09 at 19:40
1  
exactly what is a bad assumption? – Server Horror Jun 24 '09 at 23:03
feedback

Are you looking for the external IP address of the router?

In the past I've used a screen scraping script to get that sort of thing from the router setup pages. Most home routers have a browser based setup that is easy to access from the inside. It's hard to give a general solution but you can use curl or wget to fetch the page and then use grep & awk to get the IP address.

link|improve this answer
I should have been clearer in my question. Basically, I want to find the gateway IP address that is set to eth0 on a box that has a single ethernet interface. Coincidentally, this should be the IP address of a home, basic, Linksys-type or so, NAT router. I need it so I can ping it in a shell script. – J. Polfer Jun 24 '09 at 18:43
Seems more complex than what I need, but a cool, guaranteed-to-work idea though. – J. Polfer Jun 24 '09 at 18:44
feedback

netstat -rn |awk '{if($1=="0.0.0.0") print $2}'

this will cleanly print the gateway IP. (what would linux scripting be without awk?)

link|improve this answer
Then you'd have to use sed instead... netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; } – David Dec 20 '11 at 15:53
feedback

anyone shorter than this? =)

ip r | awk '/^def/{print $3}'
link|improve this answer
1  
Challenge accepted: ip r | awk 'END{print $3}' is three characters shorter. – Kenny Rasschaert Dec 20 '11 at 7:13
nice, but on my local debian/squeeze box with KVM bridges my default gw is in line one. strange but true. So your cool solution doesn`t work everywhere :-/ (anyhow +1 ) – ThorstenS Dec 20 '11 at 7:27
ip is not a generic UNIX command - so again it won't work everywhere. – David Dec 20 '11 at 15:55
you are right, but in $Subject stands Linux and not UNIX ;) @David: wow, you have really nice blog! – ThorstenS Dec 20 '11 at 17:06
feedback
[rajat@chida /]$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     *               255.255.255.0   U         0 0          0 eth0
192.168.122.0   *               255.255.255.0   U         0 0          0 virbr0
default         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
[rajat@chida /]$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
[rajat@chida /]$ 
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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