Created
January 10, 2012 18:41
-
-
Save evandhoffman/1590440 to your computer and use it in GitHub Desktop.
Redirect counter. Follows a URL n times and reports on the landing URL and how many times it hit each one (if > 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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' => '[email protected]' | |
| ); | |
| 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