Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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 www.example.com 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
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/ 192.168.1.1 192.168.1.2/; 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:

    192.168.1.1

    192.168.1.2

    etc ...

    and call it ip.txt

    #!/usr/bin/perl # test_url.pl 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 | test_url.pl 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 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. www.example.com). 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 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', 'http://192.168.12.34:80/');

    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932262]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-10-23 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls