script/cisco-intf.pl untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 209 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 Cisco::Management;
9  
10 my %opt;
11 my ($opt_help, $opt_man);
12  
13 GetOptions(
14 'community=s' => \$opt{community},
15 'down!' => \$opt{down},
16 'interfaces=s' => \$opt{interfaces},
17 'metrics:s' => \$opt{metrics},
18 'repeat!' => \$opt{repeat},
19 'up!' => \$opt{up},
20 'Utilization:i' => \$opt{util},
21 'help!' => \$opt_help,
22 'man!' => \$opt_man
23 ) or pod2usage(-verbose => 0);
24  
25 pod2usage(-verbose => 1) if defined $opt_help;
26 pod2usage(-verbose => 2) if defined $opt_man;
27  
28 # Make sure at least one arg was provided
29 if (!@ARGV) {
30 pod2usage(-verbose => 0, -message => "$0: host required\n")
31 }
32  
33 $opt{community} = $opt{community} || 'private';
34  
35 my $oper;
36 if (defined($opt{up})) {
37 $oper = 'UP'
38 } elsif (defined($opt{down})) {
39 $oper = 'DOWN'
40 }
41  
42 for (@ARGV) {
43 print "\n-- $_ --\n";
44  
45 my $cm;
46 if (!defined($cm = Cisco::Management->new(
47 hostname => $_,
48 community => $opt{community}
49 ))) {
50 printf "Error: %s\n", Cisco::Management->error;
51 next
52 }
53  
54 my %params;
55 my $ERROR = 0;
56 if (defined($opt{interfaces})) {
57  
58 if ($opt{interfaces} =~ /^[A-Za-z]/) {
59 my @temp;
60 my @ifs = split /\s+/, $opt{interfaces};
61 for (@ifs) {
62 if (defined(my $if = $cm->interface_getbyname(interface => $_, index => 1))) {
63 push @temp, $if
64 } else {
65 printf "Error: %s\n", Cisco::Management->error;
66 $ERROR = 1;
67 last
68 }
69 }
70 $params{interfaces} = join ',', @temp
71 } else {
72 $params{interfaces} = $opt{interfaces}
73 }
74 }
75  
76 next if ($ERROR);
77  
78 if (defined($oper)) {
79 $params{operation} = $oper;
80 if (defined(my $ifs = $cm->interface_updown(%params))) {
81 print "$_: Admin $oper interfaces = @{$ifs}\n"
82 } else {
83 printf "Error: %s\n", Cisco::Management->error
84 }
85 } elsif (defined($opt{metrics}) && !defined($opt{util})) {
86 if ($opt{metrics} ne '') {
87 my @temp;
88 push @temp, split /\s+/, $opt{metrics};
89 $params{metrics} = \@temp
90 }
91 if (defined(my $ifs = $cm->interface_metrics(%params))) {
92 print "Index Multi(I/O) Broad(I/O) Octets(I/O)\n";
93 print "-----------------------------------------\n";
94 for my $int (sort {$a <=> $b} (keys(%{$ifs}))) {
95 printf "%5i %5s/%-5s %5s/%-5s %5s/%-5s\n",
96 $int,
97 defined($ifs->{$int}->{InMulticasts}) ? $ifs->{$int}->{InMulticasts} : '-',
98 defined($ifs->{$int}->{OutMulticasts}) ? $ifs->{$int}->{OutMulticasts} : '-',
99 defined($ifs->{$int}->{InBroadcasts}) ? $ifs->{$int}->{InBroadcasts} : '-',
100 defined($ifs->{$int}->{OutBroadcasts}) ? $ifs->{$int}->{OutBroadcasts} : '-',
101 defined($ifs->{$int}->{InOctets}) ? $ifs->{$int}->{InOctets} : '-',
102 defined($ifs->{$int}->{OutOctets}) ? $ifs->{$int}->{OutOctets} : '-'
103 }
104 } else {
105 printf "Error: %s\n", Cisco::Management->error
106 }
107 } elsif (defined($opt{util})) {
108 if ($opt{util} > 0) { $params{polling} = $opt{util} }
109 if (defined($opt{metrics}) && $opt{metrics} ne '') {
110 my @temp;
111 push @temp, split /\s+/, $opt{metrics};
112 $params{metrics} = \@temp
113 }
114  
115 print "Index Multi(I/O) Broad(I/O) Octets(I/O)\n";
116 print " packets/s packets/s bits/s\n";
117  
118 my $stopRepeat = 0;
119 $SIG{INT} = sub {
120 print "SIGINT! - Stop\n";
121 $stopRepeat = 1
122 };
123  
124 my $recur;
125 my $GO = 1;
126 while ($GO) {
127 my $ifs;
128 $params{recursive} = $recur;
129 ($ifs, $recur) = $cm->interface_utilization(%params);
130 if (defined($ifs)) {
131 print "-----------------------------------------\n";
132 for my $int (sort {$a <=> $b} (keys(%{$ifs}))) {
133 printf "%5i %5s/%-5s %5s/%-5s %5s/%-5s\n",
134 $int,
135 defined($ifs->{$int}->{InMulticasts}) ? $ifs->{$int}->{InMulticasts} : '-',
136 defined($ifs->{$int}->{OutMulticasts}) ? $ifs->{$int}->{OutMulticasts} : '-',
137 defined($ifs->{$int}->{InBroadcasts}) ? $ifs->{$int}->{InBroadcasts} : '-',
138 defined($ifs->{$int}->{OutBroadcasts}) ? $ifs->{$int}->{OutBroadcasts} : '-',
139 defined($ifs->{$int}->{InOctets}) ? $ifs->{$int}->{InOctets} : '-',
140 defined($ifs->{$int}->{OutOctets}) ? $ifs->{$int}->{OutOctets} : '-'
141 }
142 } else {
143 printf "Error: %s\n", Cisco::Management->error
144 }
145 if (!defined($opt{repeat}) || $stopRepeat) { last }
146 }
147 } else {
148 if (defined(my $ifs = $cm->interface_info(%params))) {
149 my $ips = $cm->interface_ip();
150 print "Index Description Speed/Duplex Admin/Oper IP(s)\n";
151 print "--------------------------------------------------------------\n";
152 for my $int (sort {$a <=> $b} (keys(%{$ifs}))) {
153 printf "%5i %-25s %4i/%-7s %4s/%-4s ",
154 $int,
155 $ifs->{$int}->{Description},
156 ($ifs->{$int}->{Speed} > 1000000) ? $ifs->{$int}->{Speed}/1000000 : $ifs->{$int}->{Speed},
157 $ifs->{$int}->{Duplex},
158 $ifs->{$int}->{AdminStatus},
159 $ifs->{$int}->{OperStatus};
160 if (exists($ips->{$int})) {
161 for (0..$#{$ips->{$int}}) {
162 print " $ips->{$int}->[$_]->{IPAddress}"
163 }
164 }
165 print "\n"
166 }
167 } else {
168 printf "Error: %s\n", Cisco::Management->error
169 }
170 }
171 $cm->close()
172 }
173  
174 __END__