2012年12月23日 星期日

Cisco Perl threads program to manage numeric Cisco equipments

Cisco Perl Telnet Script:

A single thread Perl program fetches Cisco routers/switches config or other info may take 2 seconds per equipment. If you have 1,000 Cisco routers/switches, it’ll take 2,000 seconds which will be 33+ mins. This Perl threads program will save tons of your time to gather info like within 10 seconds. Please enjoy!
1. Successful messages( scalar type ) are stored in hash of hash : $hostinfo{ “$hostname” }->{ “$cmd” }
2. Failed messages( scalar type ) are stored in hash of hash : $hosterrinfo{ “$hostname” }->{ “$cmd” }
#!/usr/bin/perl -w
# This perl script is main to be doing numeric cisco equipment management

use strict;
use threads;
use threads::shared;

my @hosts = ( 'host name or IP', 'host name or IP' );
my @threads = ();

# Shared data declaration

my @cmds :shared = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );
my $username :shared = 'username';  # cisco telnet username
my $passwd :shared = 'password';   # cisco telnet username
my $enable_passwd :shared = 'enable password';
my %hostinfo :shared = ();
my %hosterrinfo :shared = ();

foreach my $host ( @hosts )
{
$hostinfo{ "$host" } = &share( {} );
$hosterrinfo{ "$host" } = &share( {} );
push( @threads, threads->new( \&get_info, $host ) );
}
foreach ( @threads )
{
$_->join();
}

sub get_info
{
use Net::Telnet::Cisco;  #Special Perl Cisco Telnet module
my ( $hostname ) = @_;

my $cisco = Net::Telnet::Cisco->new( 'Host' => $hostname,
'Errmode'=> 'return' );  #Perl Cisco Telnet Object initialization
$cisco->login( Password => "$passwd" );
$cisco->ignore_warnings;

foreach my $cmd ( @cmds )
{
my @output = $cisco->cmd( "$cmd" );

if( $cisco->errmsg() )
{
$hosterrinfo{ $hostname }->{ $cmd } = $cisco->errmsg();
}
else
{
my $out = join( "", @output );
$hostinfo{ $hostname }->{ $cmd } = $out;
}
}
}

foreach my $host ( keys %hostinfo )
{
foreach ( keys %{$hostinfo{ $host }} )
{
print " $host => $_ => $hostinfo{ $host }->{ $_ }";
}
}

More technical tips see:http://www.geego.com/tech_support/new-tip-list

沒有留言:

張貼留言