Joachim Breitner's Homepage
I often find myself running
/sbin/route to get the IP address of the current gateway, especially when using a wireless LAN while traveling. For example, if the “Internet does not work” I usually ping the local gateway to see where the connectivity problem lies. I also need the IP if I want to access the routers configuration web interface. This is somewhat tedious, so I wrote libnss-gw-name, and now:
$ sudo apt-get install libnss-gw-name [...] $ ping gateway.current PING gateway.current (172.20.239.1) 56(84) bytes of data. 64 bytes from hhicalvin.stud.uni-karlsruhe.de (172.20.239.1): icmp_seq=1 ttl=64 time=2.16 ms 64 bytes from hhicalvin.stud.uni-karlsruhe.de (172.20.239.1): icmp_seq=2 ttl=64 time=1.48 ms 64 bytes from hhicalvin.stud.uni-karlsruhe.de (172.20.239.1): icmp_seq=3 ttl=64 time=2.73 ms ^C --- gateway.current ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 1.482/2.129/2.739/0.513 ms
Once libnss-gw-name is installed, it hooks into the system’s Name Service Switch, which is, among other things, responsible for resolving hostnames to ip addresses. It will only react on the name “gateway.current”, checking the system’s routing table and returning the IP address of the current default gateway.
Update: Changed the name to gateway.localhost, as that is within a reserved top-level-domain.