up vote 3 down vote favorite
1
share [g+] share [fb]

Good day!

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.

Any answers will be much appreciated. =)

link|improve this question
feedback

2 Answers

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

Should do what you want.

link|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
feedback

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.

link|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
feedback

Your Answer

 
or
required, but never shown

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