Perl WWW::Mechanize: How can I specify the destination IP address independently of the URL?
Asked Answered
R

1

5

I want to use Perl www::mechanize to connect to the webserver and request a resource. E.g. http://www.my.domain/test.html. But I want to specify the IP address independently from the hostname in the URL.

For example: www.my.domain resolves to 1.1.1.1, but I want to connect to 2.2.2.2.

I want to do this to test multiple web servers behind a load balancer.

Referential answered 29/1, 2021 at 16:10 Comment(0)
H
6

use LWP::UserAgent::DNS::Hosts;

It works fine with WWW::Mechanize.

use LWP::UserAgent::DNS::Hosts;
use WWW::Mechanize;

LWP::UserAgent::DNS::Hosts->register_host('www.my.domain' => '2.2.2.2');
LWP::UserAgent::DNS::Hosts->enable_override;

my $mech = WWW::Mechanize->new;
$mech->get('http://www.my.domain/test.html'); # connects to 2.2.2.2

Holbrook answered 29/1, 2021 at 16:46 Comment(2)
I had to add LWP::UserAgent::DNS::Hosts->enable_override; to make it work.Referential
You're right, I missed that. Added it to the answer.Holbrook

© 2022 - 2024 — McMap. All rights reserved.