A PERL threads program manages massive Cisco equipment

A single thread PERL program fetches configuration or other info from Cisco routers/switches may take 2 seconds per equipment. If you have 1,000 Cisco equipment, it will approximately take 2,000 seconds which will be 33+ minutes. This PERL multi-thread program( using module called 'threads' ) will save tons of your time to gather info in like around 10 seconds. Please enjoy! If you have any questions, feel free to email me.

  • Successful messages( returned as scalar type ) are stored in hash of hash : $hostinfo{ "$hostname" }->{ "$cmd" }
  • Failed messages( returned as scalar type ) are stored in hash of hash : $hosterrinfo{ "$hostname" }->{ "$cmd" }
#!/usr/bin/perl -w 
# This perl threads script is mainly to be doing massive Cisco equipment management.

use strict;  
use threads; # PERL threads module  
use threads::shared; # PERL threads ::shared module is used to share global variable

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

# Shared data declaration

my @cmds :shared = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );  
my $username :shared = 'login account';  # cisco telnet account  
my $passwd :shared = 'replace this with your password';     # cisco telnet password  
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 Telnet module from CPAN
    my ( $hostname ) = @_;
    my $cisco = Net::Telnet::Cisco->new( 'Host' => $hostname, 
                                         'Errmode'=> 'return' );  # Instantiate a PERL Cisco Telnet object
    $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 }->{ $_ }"; 
    } 
}