Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Strawberry Perl: Windows hosts file not working?

by sectokia (Pilgrim)
on Feb 07, 2022 at 06:23 UTC ( [id://11141187]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

If I put an entry in windows hosts file (127.0.0.1 foo.example.com) then I can ping foo.example.com at command line, but nslookup foo.example.com fails.

And also all perl scripts that try and use foo.example.com will fail. Example tcp_connect of AnyEvent::Socket will say 'No such device or address'.

I cannot seem to figure out how to create a name that just points to 127.0.0.1 that I can use in URLs for scripts running on localhost. It seems like nothing in perl can resolve the name to the host. Does perl use the windows hosts file?

Replies are listed 'Best First'.
Re: Strawberry Perl: Windows hosts file not working?
by Discipulus (Canon) on Feb 07, 2022 at 08:02 UTC
    Hello sectokia,

    it seems.. no

    > Perl does not "use the /etc/hosts file". It's using the same network stack as the rest of the machine. It is the network layer that is using /etc/hosts.

    PS I confirm that it should work: test it this way

    # HOSTS # 127.0.0.1 foo.example.com perl -MSocket -E"say inet_ntoa(inet_aton($ARGV[0]))" foo.example.com 127.0.0.1

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Strawberry Perl: Windows hosts file not working?
by afoken (Chancellor) on Feb 07, 2022 at 20:32 UTC
    If I put an entry in windows hosts file (127.0.0.1 foo.example.com) then I can ping foo.example.com at command line, but nslookup foo.example.com fails.

    nslookup explicitly does NOT use the hosts file, because it is designed to do Name Server LOOKUPs. Almost all other software relies on the name resolution functions that DO use the hosts file.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Strawberry Perl: Windows hosts file not working?
by Anonymous Monk on Feb 07, 2022 at 11:52 UTC
Re: Strawberry Perl: Windows hosts file not working?
by sectokia (Pilgrim) on Feb 16, 2022 at 02:42 UTC
    The issue turned out to be that on Windows 'AnyEvent::Socket' parses the hosts file in its own code, which doesn't handle CR LF in windows host files.

      Did you open an issue for that problem?

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Strawberry Perl: Windows hosts file not working?
by enemyofthestate (Monk) on Feb 13, 2022 at 22:46 UTC

    I can vouch that gethostbyname will use the hosts file on Linux.

    This one-liner resolves a hostname to an IPv4 address:

    perl -w -MSocket -e 'my $pip=gethostbyname("www.google.com");my $ip=in +et_ntoa($pip);print $ip,"\n";'

    "Normal" resolution via DNS returns: 142.250.72.132

    If I add this line to /etc/hosts:
    127.0.0.1 www.google.com

    The above one-liner returns: 127.0.0.1

    So on Linux, Perl will use the hosts file (Well, technically, it use the resolver library). However, I don't have a Windows machine with Perl handy to test with so I do not know if it will also work there. No good reason it shouldn't.

      wouldn't that depend on what is defined in /etc/nsswitch.conf and/or /etc/host.conf? At least that's what man gethostbyname says.

        Yes, that is true. I haven't changed nsswitch.conf from the default for so long I'd forgotten about it.

        Mea Culpa

      However, I don't have a Windows machine with Perl handy to test with so I do not know if it will also work there.

      I couldn't get your example to work on Windows 7 with Strawberry Perl-5.32.0:
      C:\etc>type hosts 127.0.0.1 www.google.com.au C:\etc>perl -MSocket -wle "my $pip=gethostbyname('www.google.com.au'); +my $ip=inet_ntoa($pip);print $ip;" 142.250.66.195
      Maybe the hosts file needs to be elsewhere and/or named something else. (I also made a copy of etc/hosts named etc/hosts.txt.)

      Sorry ... I don't have much knowledge on this sort of stuff ... just reporting what I found.

      Cheers,
      Rob
        For such things I used C:\Windows\System32\drivers\etc\hosts in the past.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-20 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found