Perl で URL の HTTP Status (200 OK とか 404 Not Found とか)を確認する。
#!/usr/bin/perl use strict; use warnings; use LWP; use HTTP::Status; my $ua = LWP::UserAgent->new(); sub get_http_status { my ($url, $ua) = @_; return unless $url or $ua; my $response = $ua->head($url); my $msg = status_message($response->code); return $msg; } my @urls = qw( http://basicwerk.com/ http://basicwerk.com/memo.cgi http://basicwerk.com/contact.html http://basicwerk.com/not_found.html ); foreach my $url (@urls) { print "$url\t"; print get_http_status($url, $ua); print "\n"; } # 出力結果はこんな感じ # http://basicwerk.com/ OK # http://basicwerk.com/memo.cgi OK # http://basicwerk.com/contact.html OK # http://basicwerk.com/not_found.html Not Found
例えばこれを任意の URL を引数に受け取って結果を返す get_http_status.pl のようにするなら、@urls の部分を @ARGV に置き換えて、
#!/usr/bin/perl use strict; use warnings; use LWP; use HTTP::Status; my $ua = LWP::UserAgent->new(); sub get_http_status { my ($url, $ua) = @_; return unless $url or $ua; my $response = $ua->head($url); my $msg = status_message($response->code); return $msg; } foreach my $url (@ARGV) { print "$url\t"; print get_http_status($url, $ua); print "\n"; }
$ chmod 0755 get_http_status.pl $ get_http_status.pl http://basicwerk.com/ http://basicwerk.com/not_found.html http://basicwerk.com/ OK http://basicwerk.com/not_found.html Not Found