Joachim Breitner

libnss-gw-name: A stable name for your gateway

Published 2010-03-18 in sections English, Digital World.

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.

It’s a pretty simple and small tool, but it could well prove very handy to the power user. I uploaded libnss-gw-name to Debian sid, you can download the source code or access the git repository.

Update: Changed the name to gateway.localhost, as that is within a reserved top-level-domain.

Comments

What happens if there are two default routes with different metrics, as done with dhcpcd 5.x when both a wired and wireless connections are present?
#1 Alexander E. Patrakov am 2010-03-19
It will return the first one, because it is convenient. One could implement something round-robin-like, but I’m not sure if there is actually a use case for it.



What would you like libgw-name return?
#2 Joachim Breitner (Homepage) am 2010-03-19
Very nice.



Could I talk you into also packaging nss-myhostname (http://0pointer.de/lennart/projects/nss-myhostname/)? It resolves the system's current hostname to 127.0.0.2 and ::1 so you don't need to edit /etc/hosts when you change the system's hostname.



(I'd love to see Debian use it by default, for exactly that reason: one less file that varies from system to system, and one less file to edit if you change the hostname.)
#3 Josh Triplett (Homepage) am 2010-03-19
Hmm, you could maybe, but I’m not very eager. Is there a RFP for it already?
#4 Joachim Breitner (Homepage) am 2010-03-19
I thought I'd filed an RFP, but apparently not. In any case, I filed one now. Awaiting a bug number. :)



Alternatively, if you'd rather not package nss-myhostname, would you consider integrating functionality like it into libnss-gw-name or another nss module in the same package?
#5 Josh Triplett (Homepage) am 2010-03-21
I think I will package it, but maybe in a while – at the moment I am at the Haskell Hackathon, and after that I’ll have to finally start to prepare for exams...
#6 Joachim Breitner (Homepage) am 2010-03-21
Thanks for considering it, in any case!
#7 Josh Triplett (Homepage) am 2010-03-21

Have something to say? You can post a comment by sending an e-Mail to me at <mail@joachim-breitner.de>, and I will include it here.