#!/usr/bin/perl -w
# errdis.pl
# pod at tail
use strict;
use Net::Telnet::Cisco;
use Term::ReadKey;
use Term::ReadLine;
my %parm = (
tntimeout => 30,
pwtimeout => 60,
errmode => 'return',
);
my %file = (
in => 'errdis.in',
tmp0 => 'errdis.tmp0',
tmp1 => 'errdis.tmp1',
out => 'errdis.out',
);
my @tmpfiles = qw(
$file{tmp0}
$file{tmp1}
);
my @commands = (
'set leng 0', # CatOS no '--More--' scrolling
'sho port st', # meat n' potatos
);
my @keeplines = ( # lines of text to be kept
'sho port st',
'errdis',
# 'auto',
# 'connected',
# ' 10 ',
# ' 100 ',
);
######################################################################
+####
Usage() unless ((@ARGV) or (-s $file{in} && -T_));
# $file{tmp1} must be present or errors if first device unreachable
open (TMP1, ">$file{tmp1}") or die "Error opening $file{tmp1} WO: $!";
close (TMP1) or die "Error closing $file{tmp1}: $1";
######################################################################
+####
# noecho *before* prompting for password - isotope
# don't need privilaged mode for 'sho port st'
# Win32 - "Non-blocking ReadLine is not supported on this architecture
+"
ReadMode('noecho');
print "\n",
" Prompting for password\n",
" (*not* echoed to screen nor written to disk)\n\n",
' Enter password: ';
my $pass;
if ($^O eq 'MSWin32') {
chomp ($pass = <STDIN>);
} else {
$pass = ReadLine($parm{pwtimeout});
}
ReadMode('restore');
unless (defined($pass)) {
print "\n\nSorry, you waited too long to enter the password.\n\n";
exit;
}
######################################################################
+####
unless (@ARGV) {
print "\n Using $file{in} for input\n";
@ARGV = $file{in};
chomp (@ARGV = <>);
}
######################################################################
+####
print "\n Starting port status check, shouldn't take long...\n";
for my $cisco (@ARGV) {
if (my $cs=Net::Telnet::Cisco->new(
host => $cisco,
timeout => $parm{tntimeout},
errmode => $parm{errmode},
input_log => $file{tmp0},
)
)
{
$cs->login('',$pass); # vty login
print ' ', $cs->last_prompt, ' '; # print to console
for(@commands) {
my @output = $cs->cmd($_);
}
print $cs->last_prompt, "\n"; # print to console
$cs->close; # exit session
open (TMP0, "<$file{tmp0}")
or die "Error opening $file{tmp0} RO: $!";
open (TMP1, ">>$file{tmp1}")
or die "Error opening $file{tmp1} for append: $!";
while (<TMP0>) { print TMP1 $_; }
close (TMP0) or die "Error closing $file{tmp0}: $
+!";
close (TMP1) or die "Error closing $file{tmp1}: $
+!";
} else { warn "Error connecting to $cisco\n"; } # if telnet connec
+t fail
}
unlink ($file{tmp0}) or die "Error unlinking $file{tmp0}:
+ $!";
print " Finished port status check.\n";
######################################################################
+####
print " Extracting lines of interest...\n";
open (TMP1, "<$file{tmp1}") or die "Error opening $file{tmp1} RO: $!"
+;
open (OUT, ">$file{out}") or die "Error opening $file{out} WO: $!";
while (<TMP1>) {
foreach my $keep(@keeplines) {
if (/$keep/i) {
print OUT $_;
print;
}
}
}
close (TMP1) or die "Error closing $file{tmp1}: $!";
unlink ($file{tmp1}) or die "Error unlinking $file{tmp1}: $!";
close (OUT) or die "Error closing $file{out}: $!";
######################################################################
+####
print
" Finished parsing results.\n",
" Output at $file{out}\n\n\a";
if ($^O eq 'MSWin32') {
print " <enter> to exit.";
(<STDIN>);
exit;
}
######################################################################
+####
sub Usage {
print <<EOF;
D'oh! You didn't specify any target device(s) on command-line or at
+$file{in}.
If you want to try again, use the following syntax:
errdis.pl switch1 switch2 switch3
will query the 3 named (or numbered) switches for port status
errdis.pl
with no arguments will read $file{in} for list of switches to quer
+y.
$file{in} would be a text file that looks like this:
switch1
switch2
switch3
ASCII text file
One IP address or DNS name per line
FQDN if targets in different DNS domain
No leading/trailing spaces
No blank lines
Net::Telnet::Cisco $Net::Telnet::Cisco::VERSION
Net::Telnet $Net::Telnet::VERSION
Term::ReadKey $Term::ReadKey::VERSION
Perl $]
OS $^O
Program $0
EOF
exit; # exit instead of die, so no error to cons
+ole
}
######################################################################
+####
sub Pause {
print "<enter> to continue, ctrl-C to quit.\n";
(<STDIN>);
}
######################################################################
+####
=head1 Name
errdis.pl
=head1 Summary
Report on 'errdisabled' ports at Cisco CatOS LAN switch(es).
=head1 Usage
errdis.pl switch1 switch2 switch3
will query the 3 named (or numbered) switches for port status
errdis.pl
with no arguments will read $file{in} for list of switches to quer
+y.
$file{in} would be a text file that looks like this:
switch1
switch2
switch3
ASCII text file
One IP address or DNS name per line
FQDN if targets in different DNS domain
No leading/trailing spaces
No blank lines
=head1 Tested
with: Perl 5.00504 Debian 2.2r3
ActivePerl 5.6.1.626 Win2kPro
against: Cisco Catalyst 6000, 5000, 4000, 2948g
=head1 Updated
2001-05-07 16:00
List contents of outfile to console.
2001-05-04 10:55
Minor format tweaks.
2001-05-03 17:30
Initial working code (started from existing cdp-n.pl).
Posted to PerlMonks.
=head1 ToDos
Prompt for (or accept switches using GetOpt::Long)
(errdis|auto|connected| 10 | 100 ).
Debug non-fatal error only seen on Win32:
Argument "" isn't numeric in number gt (>> at Telnet.pm line 2569,
+<STDIN> line 1.
Use File::Slurp to append log (avoid race condition).
Use File::Temp instead of multiple temp files.
=head1 Author
ybiC
=cut
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.