script/cisco-ping.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 114 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5 use Getopt::Long qw(:config no_ignore_case); #bundling
6 use Pod::Usage;
7  
8 use IO::Socket;
9 use Sys::Hostname;
10 use Cisco::Management;
11  
12 my %opt;
13 my ($opt_help, $opt_man);
14  
15 GetOptions(
16 'community=s' => \$opt{community},
17 'Count=i' => \$opt{count},
18 'destination=s' => \$opt{destination},
19 'size=i' => \$opt{size},
20 'vrf=s' => \$opt{vrf},
21 'wait=i' => \$opt{wait},
22 'help!' => \$opt_help,
23 'man!' => \$opt_man
24 ) or pod2usage(-verbose => 0);
25  
26 pod2usage(-verbose => 1) if defined $opt_help;
27 pod2usage(-verbose => 2) if defined $opt_man;
28  
29 # Make sure at least one arg was provided
30 if (!@ARGV) {
31 pod2usage(-verbose => 0, -message => "$0: host required\n")
32 }
33  
34 $opt{community} = $opt{community} || 'private';
35 $opt{destination} = $opt{destination} || inet_ntoa((gethostbyname(hostname))[4]);
36 $opt{count} = $opt{count} || 4;
37 $opt{size} = $opt{size} || 64;
38 $opt{wait} = $opt{wait} || 1;
39  
40 for (@ARGV) {
41 print "\n-- $_ --\n";
42  
43 my $cm;
44 if (!defined($cm = Cisco::Management->new(
45 hostname => $_,
46 community => $opt{community}
47 ))) {
48 printf "Error: %s\n", Cisco::Management->error;
49 next
50 }
51  
52 $|++;
53 my $dest = inet_ntoa((gethostbyname($opt{destination}))[4]);
54 print "Pinging $opt{destination} [$dest] with $opt{size} bytes of data from:\n\n$_ ";
55 my %params;
56 if (defined($opt{count})) { $params{count} = $opt{count} }
57 if (defined($opt{destination})) { $params{host} = $opt{destination} }
58 if (defined($opt{size})) { $params{size} = $opt{size} }
59 if (defined($opt{wait})) { $params{wait} = $opt{wait} }
60 if (defined($opt{vrf})) { $params{vrf} = $opt{vrf} }
61 if (defined(my $pings = $cm->proxy_ping(%params))) {
62 printf "%s $opt{destination} [$dest]\n\nPing statistics for $dest:\n", ($pings->proxy_ping_received() >= 1) ? '!!! Reply from' : '... No response from';
63 printf " Packets: Sent = %i, Received = %i, Lost = %i (%i%% loss),\n", $pings->proxy_ping_sent(), $pings->proxy_ping_received(), $pings->proxy_ping_sent()-$pings->proxy_ping_received(), (100-($pings->proxy_ping_received()/$pings->proxy_ping_sent()*100));
64 print "Approximate round trip times in milli-seconds:\n";
65 printf " Minimum = %sms, Maximum = %sms, Average = %sms\n", $pings->proxy_ping_minimum(), $pings->proxy_ping_maximum(), $pings->proxy_ping_average();
66 } else {
67 printf "Error: %s\n", Cisco::Management->error
68 }
69 $cm->close()
70 }
71  
72 __END__