forked from skx/sysadmin-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-ping
executable file
·228 lines (162 loc) · 3.93 KB
/
multi-ping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env perl
=head1 NAME
multi-ping - Multi-protocol ping wrapper
=head1 SYNOPSIS
multi-ping [--loop|--forever] [--sleep=N] hostname1 hostname2 .. hostnameN
=cut
=head1 DESCRIPTION
This wrapper script will invoke one of 'ping' or 'ping6', as appropriate,
to test the connectivity of a remote host and your route to it.
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
=cut
=head1 LICENSE
Copyright (c) 2013-2014 by Steve Kemp. All rights reserved.
This script is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
#
# Check the dependencies.
#
checkSystem();
#
# Parse the command-line.
#
my %config = parsedOptions();
#
# We could parse arguments here, for the moment we will hard-wire
# a timeout of five seconds.
#
my $timeout = 5;
#
# If we didn't get any arguments then we should complain.
#
if ( scalar @ARGV < 1 )
{
print "Usage: multi-ping hostname1 hostname2 .. hostnameN\n";
exit(1);
}
#
# Process each hostname specified upon the command-line.
#
# Looping if applicable.
#
do
{
foreach my $host (@ARGV)
{
pingHost($host);
}
sleep( $config{ 'sleep' } );
} until ( !$config{ 'loop' } );
=begin doc
Given a hostname lookup both the AAAA & A records. For each result
perform the appropriate ping request.
=end doc
=cut
sub pingHost
{
my ($hostname) = (@_);
#
# If we've been given an URI then we'll remove the leading-scheme
# and use the hostname only.
#
if ( $hostname =~ m!^([a-z]+:\/\/)([^/]+)/?!i )
{
$hostname = $2;
#
# Port might be specified too, drop that if present.
#
if ( $hostname =~ /^(.*):([0-9]+)$/ )
{
$hostname = $1;
}
}
#
# Lookup the IP for the name specified
#
my $res = Net::DNS::Resolver->new;
#
# The two types we'll lookup.
#
# NOTE: This shouldn't be required but my laptop resolver seems to
# struggle with lookups of the type "ANY". Sigh.
#
#
foreach my $type (qw! A AAAA !)
{
my $query = $res->query( $hostname, $type );
if ($query)
{
foreach my $rr ( $query->answer )
{
my $ping_binary =
$rr->type eq "A" ? "ping" :
$rr->type eq "AAAA" ? "ping6" :
"";
if ($ping_binary)
{
my $result = system(
"$ping_binary -c1 -w$timeout -W$timeout $hostname >/dev/null 2>/dev/null"
);
print "Host $hostname - " . $rr->address() .
( ( $result == 0 ) ? " - alive" : " - FAILED" ) . "\n";
}
}
}
else
{
print "WARNING: Failed to resolve $hostname [$type]\n";
}
}
}
=begin doc
Test that we have the required perl dependencies present.
=end doc
=cut
sub checkSystem
{
my $eval = "use Net::DNS;";
## no critic (Eval)
eval($eval);
## use critic
#
# If we don't have Net::DNS we're out of luck.
#
if ($@)
{
print "The required Net::DNS module is missing. Aborting.\n";
exit(1);
}
}
=begin doc
Parse the options and return suitable values.
=end doc
=cut
sub parsedOptions
{
my %vars;
#
# Defaults
#
$vars{ 'loop' } = 0;
$vars{ 'sleep' } = 1;
exit
if (
!GetOptions( "help" => \$vars{ 'help' },
"verbose" => \$vars{ 'verbose' },
"forever" => \$vars{ 'loop' },
"loop" => \$vars{ 'loop' },
"sleep=i" => \$vars{ 'sleep' },
) );
pod2usage(1) if ( $vars{ 'help' } );
return (%vars);
}