1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # ping.pl --
|
---|
6 | # A component of the Greenstone digital library software
|
---|
7 | # from the New Zealand Digital Library Project at the
|
---|
8 | # University of Waikato, New Zealand.
|
---|
9 | #
|
---|
10 | # Copyright (C) 2001 New Zealand Digital Library Project
|
---|
11 | #
|
---|
12 | # This program is free software; you can redistribute it and/or modify
|
---|
13 | # it under the terms of the GNU General Public License as published by
|
---|
14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
15 | # (at your option) any later version.
|
---|
16 | #
|
---|
17 | # This program is distributed in the hope that it will be useful,
|
---|
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | # GNU General Public License for more details.
|
---|
21 | #
|
---|
22 | # You should have received a copy of the GNU General Public License
|
---|
23 | # along with this program; if not, write to the Free Software
|
---|
24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | #
|
---|
26 | ###########################################################################
|
---|
27 |
|
---|
28 |
|
---|
29 | BEGIN {
|
---|
30 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
31 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
32 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/Ping");
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | require LWP::UserAgent;
|
---|
37 | use parsargv;
|
---|
38 |
|
---|
39 | &main();
|
---|
40 |
|
---|
41 | sub print_usage {
|
---|
42 | print STDERR "\n";
|
---|
43 | print STDERR "ping.pl: Ping http or ftp URL to see if it's accessable.\n\n";
|
---|
44 | print STDERR " usage: $0 [options] URL\n\n";
|
---|
45 | print STDERR " options:\n";
|
---|
46 | print STDERR " -quiet Quiet operation\n\n";
|
---|
47 | }
|
---|
48 |
|
---|
49 | sub main {
|
---|
50 |
|
---|
51 | my ($quiet);
|
---|
52 | if (!parsargv::parse(\@ARGV, 'quiet', \$quiet)) {
|
---|
53 | &print_usage();
|
---|
54 | die "\n";
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (!scalar(@ARGV)) {
|
---|
58 | print STDERR "ERROR: no URL was provided\n";
|
---|
59 | die "\n";
|
---|
60 | }
|
---|
61 |
|
---|
62 | $ua = new LWP::UserAgent;
|
---|
63 | $ua->timeout(60);
|
---|
64 | $request = new HTTP::Request('HEAD', $ARGV[0]);
|
---|
65 | $response = $ua->request($request);
|
---|
66 |
|
---|
67 | if ($response->is_success) {
|
---|
68 | print STDERR "$ARGV[0] ping succeeded\n" unless $quiet;
|
---|
69 | exit 0;
|
---|
70 | } else {
|
---|
71 | print STDERR "$ARGV[0] ping failed\n" unless $quiet;
|
---|
72 | exit 1;
|
---|
73 | }
|
---|
74 | }
|
---|