forked from skx/sysadmin-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-ping
executable file
·120 lines (89 loc) · 1.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
#!/usr/bin/perl -w
=head1 NAME
multi-ping - Multi-protocol ping wrapper
=head1 SYNOPSIS
multi-ping hostname
=cut
=head1 DESCRIPTION
This wrapper script will invoke one of 'ping' or 'ping6', as appropriate, to test
connectivity of a remote host, or your route to it.
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
=cut
=head1 LICENSE
Copyright (c) 2013 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;
#
# The host to ping
#
my $HOST = shift;
if ( !$HOST )
{
print "Usage: multi-ping hostname\n";
exit(1);
}
#
# Check the dependencies.
#
checkSystem();
#
# Lookup the IP for the name specified
#
my $res = Net::DNS::Resolver->new;
my $query = $res->query( $HOST, "ANY" );
if ($query)
{
foreach my $rr ( $query->answer )
{
if ( $rr->type eq "A" )
{
if ( system("ping -c 1 $HOST >/dev/null 2>/dev/null") == 0 )
{
print "Host $HOST - " . $rr->address() . " alive\n";
}
else
{
print "Host $HOST - " . $rr->address() . " FAILED\n";
}
}
elsif ( $rr->type eq "AAAA" )
{
if ( system("ping6 -c 1 $HOST >/dev/null 2>/dev/null") == 0 )
{
print "Host $HOST - " . $rr->address() . " alive\n";
}
else
{
print "Host $HOST - " . $rr->address() . " FAILED\n";
}
}
}
}
else
{
print "Failed to resolve $HOST\n";
exit(1);
}
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 "Mising Net::DNS module\n";
exit(1);
}
}