Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In my system I have eth0 (which may or may not be connected) and a modem on ppp0 (which always may be up or down). The the case where both interfaces are up and ppp0 is the default route, I'd like to find a way to determine the gateway IP actual address of eth0. I tried "netstat -rn" but in this configuration the output is:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
xx.xx.xxx.xxx   0.0.0.0         255.255.255.255 UH        0 0          0 ppp0
192.168.98.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 ppp0

So how do I determine eth0's gateway address? In the above case the actual gateway address is 192.168.98.1.

share|improve this question
    
What is "eth0's gateway address" supposed to be? –  Hauke Laging Apr 11 at 16:52
1  
Interfaces don't have gateway addresses, netblocks do. A gateway address tells the kernel where to forward packets destined for some host on some network. If the address is associated with a given interface then that interface will be used, but the address is independent of the interface. –  Kyle Jones Apr 11 at 16:52
    
So in my application I am supposed to make eth0 the default gateway if it has connectivity. But to check connectivity I make a temporary route to my known good server. But to do this I need to know eth0's gateway address. So how can I determine this? –  fred basset Apr 11 at 17:04

2 Answers 2

up vote 1 down vote accepted

Assume eth0 is dhcp client interface.
One of options is to check dhcp client lease files dhcpd.leases
Place and name depend on system, some fedora box under /var/lib/dhclient/ are lease files, where interesting string is like that :

  option routers 192.168.1.1;

Another option, which worked for me on funtoo box: dhcpcd -U eth0
prints nice table, ready to source in scripts

broadcast_address=192.168.1.255
dhcp_lease_time=86400
dhcp_message_type=5
dhcp_server_identifier=192.168.1.1
domain_name_servers='192.168.1.1 192.168.1.101'
ip_address=192.168.1.101
network_number=192.168.1.0
routers=192.168.1.1
subnet_cidr=24
subnet_mask=255.255.255.0

There also another options like dhcping, dhclient -n according google and manpages, but they fails on my boxes, but may work for you.

share|improve this answer

Your mistake is that you're associating the information on the netstat output with the interface rather than the destination. Destination addresses can have associated gateways. When you configure your network, you're associating interfaces and gateways with sets of destination addresses -- so the question you need to be asking is "what's the gateway for destinations in 192.168.98/24?" I'm betting there isn't supposed to be one, that devices in that range are either bridged or directly connected to that ethernet.

Notice that your default route is via ppp0 (the last line is a wildcard route, the mask is all zero so no bits are actually compared, so packets for those addresses are sent via ppp0). So packets for any destination not in 127.xx.xx.xx or 192.168.98.xx will eventually match the wildcard mask and be sent over ppp0. Now, because it's a point-to-point protocol, every packet sent on that interface goes to the only device directly addressable on that link, the one at the other end of it. There's no need to specify a gateway for it because every packet is sent over that interface is sent via that device, whether its Internet destination is that device or not.

Now: if you have a router accessible over eth0 that you use when you can, that router almost certainly has an IP address in the 192.168.98.xx range, at least as likely as not 192.168.98.1, and when that router's powered up and actually connected to the Internet you want your default route, the one for everything not directly connected to a local network, to be default via 192.168.98.1

You might have the newer (that's a relative term, it's been around for a very long time now) ip command in use, try ip route and if that gives you some output try ip route help. Otherwise, try route help.


So, your eth0 interface simply doesn't have a gateway address. There might be another device or several devices connected to that ethernet willing to serve as a gateway. You say your network's got one that responds on the (purely conventional) .1. No law says it has to have that address, and no law says it has to advertise its presence. There are some common ways, the most common of them being for boxes running routing software to also run dhcp servers and tell anyone who speaks dhcp about themselves that way. MolbOrg's answer gives a pretty typical flavor of the setup, but anything anyone can suggest at this point is only going to be more or less likely to match your particular network.

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.