Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

request URL from a specified IP address

by butchie3980 (Acolyte)
on Oct 19, 2011 at 00:37 UTC ( #932262=perlquestion: print w/replies, xml ) Need Help??
butchie3980 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all:
I wanted to test an http get from an internal web server that is serving up several domains.
the idea is to do the request to the IP, but ask for the URL
one way to test is to edit the hosts file and trick my local machine into thinking is at the IP address.
That won't work because I need to test dozens of servers, and dozens of URLs, and I don't want to change a host entry multiple times
so I'd love to set up something where I specify IP address and then the URL that the server should be responding to at that IP.
is there a way to do this?
  • Comment on request URL from a specified IP address

Replies are listed 'Best First'.
Re: request URL from a specified IP address
by anneli (Pilgrim) on Oct 19, 2011 at 03:20 UTC

    You'll want to connect to the IP address, but set the Host header to the host (e.g. Assuming you're using LWP, it might look like this:

    $ua->get($url, Host => $host);

    Here, $url contains the full URL, where you can use the IP or any hostname that resolves to the correct IP, and $host contains the hostname you actually want to go to.

    Hope this helps!

Re: request URL from a specified IP address
by williams554 (Sexton) on Oct 19, 2011 at 02:33 UTC

    When I read your post you seem to be asking, "I want to test many IP addresses for a URL (or URI)" because you were thinking you could accomplish this by just editing the /etc/hosts file and then test. If I've read you're problem correctly then something like this would work or get you started.

    #!/usr/bin/perl use strict; use warnings; use LWP::Simple; my @IP = qw/; my $url = '/home/index.html'; foreach (@IP) { print "TESTING: $_$url\n"; my $reply = get("http://$_$url"); #test reply here, etc... }
    another way:

    I would probably put all of my IPs in a file:

    etc ...

    and call it ip.txt

    #!/usr/bin/perl # use strict; use warnings; use LWP::Simple; my $ip=""; my $url = '/home/index.html'; while(defined($ip=<STDIN>)) { my $reply = get("http://$ip$url"); warn "couldn't get $ip$url\n" unless defined $reply; }

    and then cat ip.txt | Something like this... Good luck, Rob

      I think you may have it backwards; the poster has one IP, but many hostnames.

        I got Kung-Fu'd. I was trying to help but got flipped into learning that the URL can be the same IP but derivate based on hostname after reading this statement. **bows humbly, Rob

Re: request URL from a specified IP address
by Sewi (Friar) on Oct 19, 2011 at 05:28 UTC

    Try to set the target IP address as proxy server for LWP:

    my $ua = LWP::UserAgent->new; $ua->proxy('http', '');

    Communication with proxy servers is slightly different from "plain" HTTP communication (mostly: the URL is part of the GET/POST request, not only the path part), but I didn't find a webserver not supporting this.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932262]
Approved by rovf
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-03-24 05:07 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (297 votes). Check out past polls.