富捷 IT 培訓 (原奇科電腦) www.geego.com.tw 是培訓與職業轉型的先驅,專門研究當今最需要的技能。也是培訓、職涯成長和職業轉換的主要資源;長年來,我們已培養了一群追求自己鍾愛職業生涯的專業人士。 意者請洽+886-2-27116373。 http://www.geego.com.tw; e-mail:service@geego.com.tw Linux LPIC-1 / Linux LPIC-2 / CCENT / CCNA /CCNP / Java OCPJP / Android App / Android Advanced / Android Security / Swift / iOS App / Python / Python x Crawler / Python x Database / Oracle Database / HTML5 / CSS3 / JavaScript / MySQL / PHP /
2013年1月14日 星期一
奇科電腦 Tech Tips 視頻教學:Perl進階 Cisco設備運用-多執行緒(thread)與平行執行緒(fork)
更多Tech Tips請參考以下網頁
http://www.geego.com.tw/tech_support/new-tip-list
奇科電腦提供台灣地區最優質、最專業的程式設計教學。
http://www.geego.com.tw/edm/progm_perl_programming_fundamental/index.php?v=blogger&c=video&k=perl&p=progm&t=programming_fundamental
意者請洽+886-2-27116373。
http://www.geego.com.tw;
e-mail:service@geego.com.tw
GeeGo Systems offer high quality and leading of training solution on Programming in Taiwan. More information about GeeGo, please call +886-2-27116373, http://www.geego.com or mail to service@geego.com.tw
2012年12月23日 星期日
Perl program uses parallel processes( fork() ) to manage numeric Cisco equipment/device
Cisco Perl Telnet Script:
A single process 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 will take 2,000 seconds which will be 33+ minutes. This Perl multi-processes program will save tons of your time to gather info like around 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 IPC::Shareable;
$SIG{ 'CHLD' } = 'IGNORE';
my @hosts = ( ''host name or IP', ''host name or IP' );
my @cmds = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );
my $username = 'username'; # cisco telnet username
my $passwd = 'password'; # cisco telnet username
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 ) # this is child process
{
print "Child process is $$\n";
get_info( $host );
exit( 0 );
}
else # This is 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; #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 my $cmd ( keys %{$hostinfo{ $host }} )
{
print "$host => $cmd =>\n $hostinfo{ $host }->{ $cmd }\n";
}
}
More technical tips see:http://www.geego.com/tech_support/new-tip-list
2012年12月18日 星期二
奇科電腦 平行多程序( fork() )的Cisco設備的Perl程式
Cisco Perl Telnet Script:
利用telnet取得每個Cisco路由器或交換器的組態或訊息所需時間約2秒鐘,如果貴公司有1,000台Cisco設備,一般單執行緒程式恐耗時 2,000秒相當於33+分鐘,本Perl程式使用平行多程序同時對所有的Cisco設備進行存取,所需時間可能不到10秒鐘;請各位試試看。1. 執行成功訊息輸出(scalar type)儲存於$hostinfo{ 主機名稱 }->{ 執行指令 }
2. 執行失敗訊息輸出(scalar type)儲存於$hosterrinfo{ 主機名稱 }->{ 執行指令 }
#!/usr/bin/perl -w
# This perl script is main to be doing numeric cisco equipment management
use strict;
use IPC::Shareable;
$SIG{ 'CHLD' } = 'IGNORE';
my @hosts = ( '主機名稱或IP定址', '主機名稱或IP定址' );
my @cmds = ( 'sh geego', 'sh ver', 'sh clock', 'sh queueing', 'sh hosts' );
my $username = '登入帳號名稱'; # cisco telnet 的帳號
my $passwd = '帳號之密碼'; # cisco telnet 的密碼
my $enable_passwd = 'enable的密碼';
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 ) # this is child process
{
print "Child process is $$\n";
get_info( $host );
exit( 0 );
}
else # This is 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; # 特別的 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 my $cmd ( keys %{$hostinfo{ $host }} )
{
print "$host => $cmd =>\n $hostinfo{ $host }->{ $cmd }\n";
}
}
更多技術小秘訣請看:http://www.geego.com.tw/tech_support/new-tip-list
訂閱:
文章 (Atom)