A PERL fork() script manages massive Cisco equipment

Cisco PERL Telnet Script:

A single process 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-processes program( using fork() ) 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 script is main to be doing massive cisco equipment management

use strict;  
use IPC::Shareable;

$SIG{ 'CHLD' } = 'IGNORE';

my @hosts = ( 'host name or IP addr', 'other host name or IP addr' );  
my @cmds = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );  
my $username = 'login account';  # cisco telnet account  
my $passwd = 'replace with your password';    # cisco telnet password  
my $enable_passwd = 'enable password';  
my %hostinfo = ();  
my %hosterrinfo = ();  
my @children = ();  
my %ipcoptions = ( create => 'yes',  
                   exclusive => 0,
                   mode => 0644,
                   destroy => 'yes' );

tie %hostinfo, 'IPC::Shareable', 'good', \%ipcoptions or die "Can’t tie %hostinfo.\n";  
tie %hosterrinfo, 'IPC::Shareable', 'err', \%ipcoptions or die "Can’t tie %hosterrinfo\n";

foreach my $host ( @hosts )  
{
    my $pid = fork();
    die "Can’t fork.:$!\n" unless defined( $pid );

    if( $pid == 0 ) # these are child processes
    {
        print "Child process is $$\n";
        get_info( $host );
        exit( 0 );
    }
    else # This is the parent process
    {
        print "$$ -> Process $pid is processing $host now.\n";
        push( @children, $pid );
    }
}

foreach ( @children )  
{
    #my $child_pid = waitpid( $_, 0 );
    my $child_pid = wait();
    print “Process $_ is done.\n";
}

sub get_info  
{
    use Net::Telnet::Cisco;  # Module that access Cisco equipment, can be obtained from CPAN
    my ( $hostname ) = @_;
    my $cisco = Net::Telnet::Cisco->new( 'Host' => $hostname,
                                         'Errmode'=> 'return' );  #Instantiate an 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 my $cmd ( keys %{$hostinfo{ $host }} )
    {
        print "$host => $cmd =>\n $hostinfo{ $host }->{ $cmd }\n";
    }
}