Cisco Perl Telnet Script:

利用telnet取得每個Cisco路由器或交換器的組態或訊息所需時間約2秒鐘,如果貴公司有1,000台Cisco設備,一般單執行緒程式恐耗時 2,000秒相當於33+分鐘,本多執行緒Perl程式可同時對所有的Cisco設備進行存取,所需時間可能不到10秒鐘;請各位試試看。

 

1. 執行成功訊息輸出(scalar type)儲存於hash of hash : $hostinfo{ 主機名稱 }->{ 執行指令 }
2. 執行失敗訊息輸出(scalar type)儲存於hash of hash: $hosterrinfo{ 主機名稱 }->{ 執行指令 }

#!/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 = ( '主機名稱或IP', '主機名稱或IP' );
my @threads = ();

# Shared data declaration

my @cmds :shared = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );
my $username :shared = '登入帳號名稱';  # cisco telnet 的帳號
my $passwd :shared = '登入密碼';     # cisco telnet 的帳號
my $enable_passwd :shared = 'enable的密碼';
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;  # 特別的 Perl Cisco Telnet的模組
my ( $hostname ) = @_;

my $cisco = Net::Telnet::Cisco->new( 'Host' => $hostname,
'Errmode'=> 'return' );  # Perl Cisco Telnet的物件初始化
$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 }->{ $_ }";
}
}

更多技術小秘訣請看:http://www.geego.com.tw/tech_support/new-tip-list

arrow
arrow

    台灣 富捷IT培訓 發表在 痞客邦 留言(0) 人氣()