Skip to content

Instantly share code, notes, and snippets.

@dlundquist
Created December 8, 2011 21:54
Show Gist options
  • Save dlundquist/1448796 to your computer and use it in GitHub Desktop.
Save dlundquist/1448796 to your computer and use it in GitHub Desktop.
DNS round robin in perl LWP
#!/usr/bin/perl
use strict;
use warnings;
use LWP;
use LWP::Protocol::http; # So we can manipulate EXTRA_SOCK_OPTS
use HTTP::Request::Common;
# LWP DNS round Robin
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request::Common::GET(shift @ARGV);
my ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($req->uri()->host());
for my $addr (@addrs) {
my ($a,$b,$c,$d) = unpack('W4',$addr);
my $ip = "$a.$b.$c.$d";
@LWP::Protocol::http::EXTRA_SOCK_OPTS = (PeerAddr => $ip);
my $res = $ua->request($req);
print $res->content() . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment