| 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 | 'Clear!' => \$opt{clear}, | ||
| 16 | 'lines=s' => \$opt{lines}, | ||
| 17 | 'message=s' => \$opt{message}, | ||
| 18 | 'quantity!' => \$opt{quantity}, | ||
| 19 | 'help!' => \$opt_help, | ||
| 20 | 'man!' => \$opt_man | ||
| 21 | ) or pod2usage(-verbose => 0); | ||
| 22 | |||
| 23 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 24 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 25 | |||
| 26 | # Make sure at least one host was provided | ||
| 27 | if (!@ARGV) { | ||
| 28 | pod2usage(-verbose => 0, -message => "$0: host required\n") | ||
| 29 | } | ||
| 30 | |||
| 31 | $opt{community} = $opt{community} || 'private'; | ||
| 32 | |||
| 33 | for (@ARGV) { | ||
| 34 | print "\n-- $_ --\n"; | ||
| 35 | |||
| 36 | my $cm; | ||
| 37 | if (!defined($cm = Cisco::Management->new( | ||
| 38 | hostname => $_, | ||
| 39 | community => $opt{community} | ||
| 40 | ))) { | ||
| 41 | printf "Error: %s\n", Cisco::Management->error; | ||
| 42 | next | ||
| 43 | } | ||
| 44 | |||
| 45 | if (defined($opt{quantity})) { | ||
| 46 | if (defined(my $lines = $cm->line_numberof())) { | ||
| 47 | print "$_: Lines = $lines\n" | ||
| 48 | } else { | ||
| 49 | printf "Error: %s\n", Cisco::Management->error | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | if (defined($opt{message})) { | ||
| 54 | my %params; | ||
| 55 | if (defined($opt{lines})) { $params{lines} = $opt{lines} } | ||
| 56 | if (defined($opt{message})) { $params{message} = $opt{message} } | ||
| 57 | if (defined(my $lines = $cm->line_message(%params))) { | ||
| 58 | print "$_: Messaged lines = @{$lines}\n" | ||
| 59 | } else { | ||
| 60 | printf "Error: %s\n", Cisco::Management->error | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | if (defined($opt{clear})) { | ||
| 65 | my %params; | ||
| 66 | if (defined($opt{lines})) { $params{lines} = $opt{lines} } | ||
| 67 | if (defined(my $lines = $cm->line_clear(%params))) { | ||
| 68 | print "$_: Cleared lines = @{$lines}\n" | ||
| 69 | } else { | ||
| 70 | printf "Error: %s\n", Cisco::Management->error | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | if ((!defined($opt{quantity})) && | ||
| 75 | (!defined($opt{message} )) && | ||
| 76 | (!defined($opt{clear} ))) { | ||
| 77 | if (defined(my $lines = $cm->line_info())) { | ||
| 78 | my $sessions = $cm->line_sessions(); | ||
| 79 | print "Line Type Active\n"; | ||
| 80 | print "----------------\n"; | ||
| 81 | for my $line (sort {$a <=> $b} (keys(%{$lines}))) { | ||
| 82 | printf "%4i %-5s ", | ||
| 83 | $line, | ||
| 84 | $lines->{$line}->{Type}; | ||
| 85 | if ($lines->{$line}->{Active} == 1) { | ||
| 86 | print "YES"; | ||
| 87 | if (exists($sessions->{$line})) { | ||
| 88 | for (0..$#{$sessions->{$line}}) { | ||
| 89 | print " ($sessions->{$line}->[$_]->{Type}://$sessions->{$line}->[$_]->{Address} [$sessions->{$line}->[$_]->{Direction}])" | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } else { | ||
| 93 | print " NO" | ||
| 94 | } | ||
| 95 | print "\n" | ||
| 96 | } | ||
| 97 | } else { | ||
| 98 | printf "Error: %s\n", Cisco::Management->error | ||
| 99 | } | ||
| 100 | } | ||
| 101 | $cm->close() | ||
| 102 | } | ||
| 103 | |||
| 104 | __END__ |