Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

/etc/hosts is not evaluated after modification on Linux

by Anonymous Monk
on May 30, 2015 at 16:47 UTC ( [id://1128413]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear colleagues, I am trying to find out, if a given hostname can be resolved and if not, I update a hosts file and check if the resolution works again. Following works fine on Windows and Solaris, but not on Linux:
#!/usr/bin/perl use strict; use warnings; our $| = 1; my $host_name = "test_host"; my $host_ip = "192.168.0.50"; #my $hosts = 'C:\Windows\System32\drivers\etc\hosts'; my $hosts = '/etc/hosts'; unless (defined gethostbyname $host_name) { print "Cannot find \"$host_name\"\n"; print "Adding $host_name to $hosts ... "; open my $fh, ">>", $hosts or die "Cannot open \"$hosts\" for a +ppending: $!\n"; print $fh "$host_ip\t$host_name\n"; close $fh; print "OK\n"; print "Checking if $host_name is known now\n"; my $packed_ip = gethostbyname $host_name || 0; print "--- $packed_ip ---\n"; }
If I start this on Linux (
SUSE Linux Enterprise Server 10 (x86_64) VERSION = 10 PATCHLEVEL = 4 perl -v This is perl, v5.8.6 built for i686-linux
) it does not work:
# /tmp/gethostbyname.pl Cannot find "test_host" Adding test_host to /etc/hosts ... OK Checking if test_host is known now --- 0 --- #
Can anyone explain why and is there any workaround? Thanks, Vadim

Replies are listed 'Best First'.
Re: /etc/hosts is not evaluated after modification on Linux
by Corion (Patriarch) on May 30, 2015 at 16:56 UTC

    My guess is that on Linux, the call to gethostbyname is cached somewhere outside the resolver and the updated information does not reach your program. I don't know enough about Linux to know how to make the C library, or the resolver deliver you the updated information immediately.

Re: /etc/hosts is not evaluated after modification on Linux
by philipbailey (Curate) on May 30, 2015 at 21:11 UTC

    As Corion says, it sounds like a caching issue. On SLES, it is likely you have an nscd daemon running, which is responsible for caching this data. According to the documentation, running this command after changing /etc/hosts will invalidate the cache and should resolve your problem, I think:

    sudo nscd -i /etc/hosts
      Indeed, following helps: system("/usr/sbin/nscd", "-i", "hosts"); Thanks a lot! -Vadim
Re: /etc/hosts is not evaluated after modification on Linux
by afoken (Chancellor) on May 31, 2015 at 06:44 UTC
    I am trying to find out, if a given hostname can be resolved and if not, I update a hosts file and check if the resolution works again.

    Very strange approach. It seems you are working in a network with private IP addresses, and have a nameserver that does not "know enough" about that network. Why don't you update the data the nameserver uses instead of messing with /etc/hosts, probably on many computers?

    Another question: How to you update records in /etc/hosts when needed, e.g. when test_host moves from 192.168.0.50 to 192.168.0.55? Do you touch each and every /etc/hosts file?

    And a third thing: There should be no underscore (_) in a hostname (see Wikipedia for the relevant RFCs). It may work, but only by chance.

    UPDATE:

    Maybe you don't have a nameserver at all? Maybe you are abusing gethostbyname() to find a hostname in /etc/hosts? If yes, consider using dnsmasq. It provides DNS and DHCP services (optionally also TFTP), and adding hostnames to the DNS service is as easy as editing /etc/hosts - literally, because you simply edit /etc/hosts on the machine running dnsmasq. dnsmasq "watches" /etc/hosts, so you don't have to do more than editing the file.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Yes there is no nameserver in this particular example and /etc/hosts is used for resolution.

      How already pointed by philipbailey nscd is used for caching and if after updating of /etc/hosts in the script cache needs to be invalidated by using "nscd -i hosts"

Re: /etc/hosts is not evaluated after modification on Linux
by vinoth.ree (Monsignor) on May 30, 2015 at 17:36 UTC

    Also look at Sys::Hostname module also.


    All is well. I learn by answering your questions...
Re: /etc/hosts is not evaluated after modification on Linux
by Anonymous Monk on May 31, 2015 at 05:33 UTC

    Unix systems tend to use NSS nowadays. If your /etc/nsswitch.conf contains a line like

    hosts:    dns [!UNAVAIL=return] files
    
    then /etc/hosts isn't normally consulted at all. The move is towards more stringent lookups & authority, I suppose.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1128413]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found