Checking URL link status with Perl

I had a file with lots of URLs but were not sure which ones where active or which links were broken. I used the following Perl script to check the status of the links.

#!/usr/bin/perl -w

#########################################
# link_check.pl - link check script     #
# Ram Prasad, 2007, ram_megalinux.net   #
#########################################

use strict;
use warnings;
#use File::Basename;
use LWP::UserAgent;

# toggle debug mode
my $DEBUG = 1;

my $browser = create_lwp_browser();

# do the work and check the remote pages
my ($working, $broken, $odd) = get_status_codes($browser);

#------------------------#
# subs, funcs and utils
#------------------------#

sub get_status_codes
{
my $browser = shift;
my $file='urls.txt';
open (INFO,$file);
my @lines = <INFO>;
close(INFO);
foreach my $p (@lines)
{
chomp($p);
my $response = $browser->get($p);
warn "Working on ", $p, "...n" if $DEBUG;
print($p."|".$response->status_line."n");
}
}

#------------------------#
sub create_lwp_browser
{
# set any LWP settings here.  agent, proxies, follow redirects etc
my $browser = LWP::UserAgent->new;
# get the proxy env variable from command line
$browser->env_proxy;
# default time out per URL checked.
$browser->timeout(30);
$browser->agent('link_check.pl/1.0');
return $browser;
}

#------------------------#

In the above example, “urls.txt” contains the list of all URLs.

One thought on “Checking URL link status with Perl”

Leave a Reply

Your email address will not be published. Required fields are marked *