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

Hi Perl Monks,
I am working on a client network and facing weird problem in one of the unix box. ****************************************************************
sub resolve($) { my($packed_ip)=@_; my $ip_address='n/a'; if (defined $packed_ip) { $ip_address=inet_ntoa($packed_ip); } return($ip_address); } sub main() { my $packed_ip = gethostbyname("web99.mytest.in.client.com"); my $resolved=resolve($packed_ip); print("web99.mytest.in.client.com: ".$resolved."\n\n"); my $packed_ip1 = gethostbyname("web99"); $resolved=resolve($packed_ip1); print("web99: ".$resolved."\n"); } main();

And we got the following results,
web99.mytest.in.client.com: 141.168.243.250
web99: 10.97.225.188

But in the /etc/hosts file, it has the following line,
141.168.243.250 web99.mytest.in.client.com web99
However, if we execute nslookup on this box, it will show
web97 $ nslookup web99 Server: 144.136.201.10 Address: 144.136.201.10#53 Non-authoritative answer: Name: web99.ebr.in.client.com Address: 10.97.225.188 web97 $ nslookup web99.mytest.in.client.com Server: 144.136.201.10 Address: 144.136.201.10#53 Non-authoritative answer: Name: web99.mytest.in.client.com Address: 141.168.243.250
cheers
Rock

Replies are listed 'Best First'.
Re: gethostbyname() problem
by ikegami (Patriarch) on Mar 28, 2009 at 04:11 UTC
    gethostbyname amd nslookup return the same result. I fail to see how this relates to Perl.
Re: gethostbyname() problem
by frieduck (Hermit) on Mar 28, 2009 at 13:52 UTC

    As ikegami has pointed out, your system name resolution isn't doing what you want, so Perl won't either, as it uses the same underlying libraries to do so.

    On many/most *nix systems, name resolution is controlled by entries in /etc/nsswitch.conf. You'll find a line there for "hosts" that will control where your system looks to resolve names, and in what order it does. For your system to look at /etc/hosts first, you'll want your file to have a line that looks like:

    hosts: files dns
    This line essentially says, "look at /etc/hosts, and if you don't find it there, use DNS".

    If your /etc/nsswitch.conf file looks right, your /etc/hosts may have other issues that prevent that line from matching.