User avatar
DCFusor
Posts: 10
Joined: Sat Feb 01, 2014 8:20 pm

Slick LAN name resolution

Sun Feb 02, 2014 9:10 pm

Well, I'm stuck with a telco ADSL router I'm not allowed to fiddle, and it insists everything connected to it use DHCP.
This means that as my many machines are power cycled, there's no guarantee they will have the same IP address they had last time. Since I do research (fusion, as you might guess from my nic), and do a lot of data acquisition to support that, I wanted to be able to use machine names in scripts to find a MySQL data base, for example, to log data to. Some people would use bind, but I found that too weighty and strange to configure. So, as I often do, I rolled my own. While I'm sure someone could do a tighter or more elegant version in C, I did this in perl - it works well enough.

There are two programs involved, both of which fork off and become daemons, and use very little CPU.
The resolving daemon writes to /etc/hosts, which is checked first before any external DNS, so my scripts can find out what's online via looking at that file, find a webserver on a machine by just using its name in the url bar of a browrser, stuff like that. No more sneaker-net to run ifconfig to find out what the IP addie of this machine is just now.

What it does is each machine broadcasts via UDP its name/address pair, about once a minute when it's up.
That daemon is called "tellem" - it tells everyone you're here and what your name is. Just in case, tellem broadcasts each machine's info at a semi-random time averaging about 1/minute. The idea was to prevent collision storms (not really needed, but...).

The second daemon is called "tellme". It keeps track of any changes to online status and updates its /etc/hosts as required. It needs to be run as /root so it can write the /etc/hosts file. I originally wrote this for a batch (around 20) PC's, but was pleasantly surprised that it works fine with the pi too, though the install procedure might be a little different, it was so easy I forgot how I did it here (I'm posting from a pi right now). IIRC, I just called them from one of the init.d scripts, where you're root anyway. Hey, this is the advanced users forum, I'm sure you'll figure it out.

The tellme daemon simple keeps a hash of what its seen, and the age since it last saw that machine. Anyone who doesn't report in 2 min or so - they get erased from /etc/hosts. Anyone new gets written into /etc/hosts right away. This makes all manner of intra-lan things "just work" - connections to databases, the web server I tend to put on each machine, and so on. /etc/hosts isn't disturbed if no change would be made. There's more info on why and how here on my own forums: http://www.coultersmithing.com/forums/v ... f=33&t=687

But here's the goodie file (latest version which really works right):
Tellsv.9.zip
Just unzip and enjoy - install procedure is for PCs but should be obvious for pi as well
(3.5 KiB) Downloaded 71 times
It sure had made my life easier (along with some other scripts that keep databases in sync - which are now a lot simpler). I've had to adopt some rather strange practices due to running on solar power - as in, I don't have any machine "always on", though what I'm working towards with the pi is to have that - I can afford its tiny power use easily even 24/7 on my "huge UPS system". Not so much a laptop or mini-itx drawing 10x the power.
Why guess when you can know? Measure!

User avatar
DougieLawson
Posts: 35814
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Slick LAN name resolution

Sun Feb 02, 2014 9:32 pm

Why not stick with running bind9 with

Code: Select all

zone "example.bogus" {
        type master;
        file "db.example.bogus";
        allow-update { 10.1.1/24; 192.168.1/24; 2001:xxxx:yyyy:zzzz::/56; 2001:uuuu:vvvv:wwww::/56; };
        journal "/var/cache/bind/db.example.bogus.jnl";
};
That allows my dhcp clients to update their zone entries in my DNS.

I can find my Laptop with:

Code: Select all

pi@pi ~ $ nslookup dougies-lenovo
Server:         192.168.1.4
Address:        192.168.1.4#53

Name:   dougies-lenovo.example.bogus
Address: 192.168.1.33

pi@pi ~ $
The other way is run avahi-daemon on each machine (pi, linux, OSx) then all machines can find each other using multicast DNS (aka mDNS, aka Bonjour). Windows can use mDNS to some extent.

It's got to be easier to use an existing wheel rather than inventing a new way to update /etc/hosts (which should be consigned to /dev/null).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
DCFusor
Posts: 10
Joined: Sat Feb 01, 2014 8:20 pm

Re: Slick LAN name resolution

Sun Feb 02, 2014 9:50 pm

I guess I'm just glad to have gotten the discussion started. What I'm using (above) works great, and is light on resources - maybe even compared to bind? Having not tried it, I don't know for certain.
At any rate, a ton of my other scripts now assume they can read in /etc/hosts, and pick from among the more desirable machines (without running around pinging and scanning, generating network traffic), and using another way would require re-writing all that (it gets pretty complex - and it sometimes takes more than one machine for some operations).

Inertia? Yup. Like rolling my own? Double yup. Thanks for the alternative!
Why guess when you can know? Measure!

User avatar
DougieLawson
Posts: 35814
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Slick LAN name resolution

Sun Feb 02, 2014 9:58 pm

DCFusor wrote:
At any rate, a ton of my other scripts now assume they can read in /etc/hosts ...
Since we're no longer in the 1970s nothing should directly read /etc/hosts.

If you use gethostbyname() or gethostbyaddr() calls then it doesn't matter whether it's in hosts or DNS (or some other resolver), the system finds the one that returns a result (or gives SERVFAIL or NXDOMAIN). None of my systems have anything other than localhost defined in /etc/hosts. The best thing with running a DNS is that I've used it to block all parts of dailymail.co.uk on my network.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
DCFusor
Posts: 10
Joined: Sat Feb 01, 2014 8:20 pm

Re: Slick LAN name resolution

Sun Feb 02, 2014 11:21 pm

I'd never heard of /etc/hosts being deprecated - in fact, it now contains ipv6 stuff in newer distros...and works with hosts.allow and .deny, iptables etc. - and every other network program on the system - it's checked first by all (and so therefore provides a spot to override everything, as well as be fast), still, so that's news to me.

But then I didn't really get going with linux till around the mid '90s or so, and not really hardcore till the past few years - I was busy writing windows ring-0 drivers and designing hardware up till then (knowing how windows works inside - is why I'm now into linux). I have, however, heard about tons of issues with bind - the attack surface for crackers is evidently immense. Not that hacking my thing is hard - but you have to know (security by obscurity - not great but...) how it works. My telco router lets in all manner of port scans etc...see them all the time with wireshark.

In fact, outside special versions for routers (usually named something else), I thought it was *bind* that was from the old days and heavily deprecated(!) except for very particular places - not for LAN use at all. I guess it depends on who you're hanging around with and what security blogs you read? Not that anyone wants to steal my raw fusor data...but there are evidently people out there who like to break things just to break them. I worry more about that, having been attacked by a few of those.
Why guess when you can know? Measure!

User avatar
DougieLawson
Posts: 35814
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Slick LAN name resolution

Sun Feb 02, 2014 11:59 pm

When you have ten machines in your network hosts is a pain.
When you have 100 machines in your network hosts is a monster pain in the a***
When you have 1000 machines in your network hosts is 100% unusable.

Hence DNS which is effectively a versatile, distributed and easy to manage database. (It's also a cash cow for the commercial domain name providers.)

/etc/hosts is not deprecated but I can't see any value in reading it directly, use the trivial services and you don't have to worry about how your IP address is resolved, it just works automagically.

Adding AAAA records to DNS for IPv6 (forward resolution isn't difficult), I have to say I've not, yet, bothered with the reverse lookup for my network.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Bazzer
Posts: 9
Joined: Sun Jan 05, 2014 11:13 am

Re: Slick LAN name resolution

Wed Feb 05, 2014 11:45 am

You could use something like nmap and work out the ip addresses from your mac addresses too and populate your DNS server from that (or hosts file) as your mac addresses won't change. Something similar (show ip and mac addresses) to the andrio app fing I read about on rasPi.tv last night from one of Alex's posts http://raspi.tv/2014/top-ten-android-ap ... spberry-pi

One use for the hosts file is protection from DNS spoofing.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: Slick LAN name resolution

Wed Feb 05, 2014 12:08 pm

Is there really no way to disable the DHCP server of your telco router/modem? You could otherwise install your own and use either ISC dhcpd + Bind or dnsmasq to keep the DNS server (or DNS cache in the case of dnsmasq) updated with the leased ip addresses.

dnsmasq is a breeze to configure compared to ISC dhcpd + bind.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

User avatar
alisonw
Posts: 2
Joined: Sat Feb 08, 2014 2:34 pm
Location: London, UK
Contact: Website

Re: Slick LAN name resolution

Sat Feb 08, 2014 3:09 pm

Really have to disagree on the 'not use /etc/hosts' front. it is the best, indeed *only*, way to provide certain functionality where local services are concerned.For instance it is the only place where "localhost" can be defined!

Yes, it can become a problem if it grows too much, but it does have the substantial advantage that it is available from the get-go at boot time and doesn't require access to a DNS server in order to progress. That alone is a reason for its continued existence.

AlisonW


btw, as the Hosts file format wasn't settled until October 1985* it would have been difficult to use /etc/hosts during the 1970s!
* https://tools.ietf.org/html/rfc952

Return to “Advanced users”