Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redirect counter. Follows a URL n times and reports on the landing URL and how many times it hit each one (if > 1)
#!/usr/bin/perl
use strict;
use warnings;
require LWP::UserAgent;
#require HTTP::Request;
require HTTP::Response;
#use Data::Dumper::Perltidy;
my $url = shift;
my $request_count = shift;
$request_count = 1 unless defined $request_count;
my $ua = LWP::UserAgent->new(
'agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Redirect Counter/0.01',
'from' => 'evandhoffman@gmail.com'
);
my %responses = ();
for (my $i = 0 ; $i < ($request_count > 0 ? $request_count : 1); $i++) {
my $response = $ua->head( $url, ':content_file' => '/dev/null' ); # junk all the output
my $rurl = $response->base;
if (exists($responses{$rurl})) {
$responses{$rurl} = $responses{$rurl} + 1;
} else {
$responses{$rurl} = 1;
}
}
print "final_url\tcount\n";
foreach my $r (keys %responses) {
print $r."\t".$responses{$r}."\n";
}
#print Dumper $response->redirects;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment