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

Say I'm on a typical Unix server that has DNS. nsswitch.conf is setup to look at the local /etc/hosts first and then DNS.

Now let's say that I want to tell a perl script "before you look at /etc/hosts and DNS, look at this alternative hosts file instead because I want to override some addresses." Is that possible?

I realize I could write things in the perl script or create a config file for the script, etc., but I'm wondering if there is a way to tell perl to resolve IP addresses differently than the common system way. (Specifically, when using LWP to get HTTP/HTTPS objects.)

  • Comment on Tell perl to use an alternate hosts file?

Replies are listed 'Best First'.
Re: Tell perl to use an alternate hosts file?
by roboticus (Chancellor) on Dec 16, 2010 at 13:44 UTC

    raindog308:

    As wwe said, just do the name resolution in your application. To do so, you need only give an IP address instead of the hostname. Something like:

    # Your host override hash my %hosts = ( 'foo.bar.com'=>'1.2.3.4', 'bar.baz.com'=>'4.3.2.1', ); . . . . . . # Get your server name from whereever my $server = XXXXXXX; # Override the server name if you have an override $server = $hosts{$server} if exists $hosts{$server}; # and pass it along to the function/service you're using... my $result = function_wanting_host($server);

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Tell perl to use an alternate hosts file?
by wwe (Friar) on Dec 16, 2010 at 08:15 UTC
    Hi raindog308, I'm not able to tell if perl has an ability to override the OS way to resolve IP addresses but would expect it is not. Most of the time people try to reuse code which is already there (and OS is nothing different as piece of code which provides some functionality to the application which runs on top of it). If you need to implement you own IP address logic you should do it inside of your application.