富捷 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
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
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
奇科電腦 Perl執行緒程式用於部署大量Cisco設備的組態或收集資訊
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
2012年8月12日 星期日
奇科電腦 具條理性的Perl教學
彭紹彬分享~
大學時我選唸的科系為非資訊相關科系,對我來說可以說是沒有程式語言的底子。目前個人工作內容以IBM產品為主的SSR-(System Services Representative)。
久而久之,覺得自己應該轉型朝軟體方向發展。因此在Google上找各大電腦補習班。看到奇科電腦-Perl的課程就有些許的動心想要報名,因為曾經自修但成效不彰,所以想體驗“老師授課”與“自修”的不同。
與課程規劃師聊過且她有提到遠距教學及補課檔。遠距教學是我選擇奇科的原因之一。畢竟下班後,搭車到台北是很累人的~~
老師的教學內容很有條理,但我覺得很棒的地方是可以一直提問,老師也很有耐心地解答。小弟是程式語言新手,到了出作業的階段遇到了瓶頸。向老師提問後。他給我的解答其中一段『多寫多試錯,會花點時間但是就會了』。這給我很大的鼓舞,所謂萬丈高樓平地起的意境也是於此。往後出的作業憑著毅力完成,然後完成的感覺很有成就感。
上完Perl實務課後,非常謝謝奇科老師們的指導讓我的程式設計之路開啟一扇門。Perl是一個很棒的程式語言,所以很期待Perl進階課程能夠開班。
歡迎到奇科官網看更多學員經驗分享 :
http://www.geego.com.tw/students_experience/
2012年4月17日 星期二
奇科電腦 Perl Mail::Sendmail 傳送 utf8字元
<更多技術小秘訣請看 http://www.geego.com.tw/tech_support/new-tips>
當看到錯誤訊息Wide character in subroutine entry at /usr/share/perl5/Mail/Sendmail.pm line 237.
原因可能為使用perl的Mail::Sendmail模組寄送中文時會出現的問題,請使用 use Encode把內容編碼後,
再寄信時把信件表頭中加入 Content-type = 'text/plain; charset="utf-8"' 即可
程式範例:
#!/usr/bin/perl
use Mail::Sendmail;
use utf8;
use Encode;
$email = 'perl.mail::sendmail@geego.com.tw';
$course = "test course";
my $message = "這是一封用來測試perl的Mail::Sendmail模組可否成功寄送utf8字元.\n";
$message = encode( "utf8", $message );
my %mail = ( To => "$email",
From => 'GeeGo Customer Service<customers@geego.com.tw>',
'Content-type' => 'text/plain; charset="utf-8"',
Subject => "Perl’s Mail::Sendmail sending text body with utf8 encoding",
Message => "$message" );
sendmail(%mail) or die $Mail::Sendmail::error;
<更多技術小秘訣請看 http://www.geego.com.tw/tech_support/new-tips>
當看到錯誤訊息Wide character in subroutine entry at /usr/share/perl5/Mail/Sendmail.pm line 237.
原因可能為使用perl的Mail::Sendmail模組寄送中文時會出現的問題,請使用 use Encode把內容編碼後,
再寄信時把信件表頭中加入 Content-type = 'text/plain; charset="utf-8"' 即可
程式範例:
#!/usr/bin/perl
use Mail::Sendmail;
use utf8;
use Encode;
$email = 'perl.mail::sendmail@geego.com.tw';
$course = "test course";
my $message = "這是一封用來測試perl的Mail::Sendmail模組可否成功寄送utf8字元.\n";
$message = encode( "utf8", $message );
my %mail = ( To => "$email",
From => 'GeeGo Customer Service<customers@geego.com.tw>',
'Content-type' => 'text/plain; charset="utf-8"',
Subject => "Perl’s Mail::Sendmail sending text body with utf8 encoding",
Message => "$message" );
sendmail(%mail) or die $Mail::Sendmail::error;
<更多技術小秘訣請看 http://www.geego.com.tw/tech_support/new-tips>
2011年12月9日 星期五
2011年11月10日 星期四
奇科電腦 輸入一個數字,算出圓周並輸出
#!/usr/bin/perl -w
print $_ = ($ARGV[0] && $ARGV[0] =~ /\d+/) ? 2 * 3.141592654 * $ARGV[0] : 'please give me a number.', "\n";
更多技術小秘訣請參考以下網頁
http://www.geego.com.tw/tech_support/new-tips
print $_ = ($ARGV[0] && $ARGV[0] =~ /\d+/) ? 2 * 3.141592654 * $ARGV[0] : 'please give me a number.', "\n";
更多技術小秘訣請參考以下網頁
http://www.geego.com.tw/tech_support/new-tips
2011年10月28日 星期五
奇科電腦 技術小秘訣: Perl 版密碼產生器(新版本)!
技術小秘訣: Perl 版密碼產生器(新版本)!
#!/usr/bin/perl
@list = (0..9, a..z, A..Z,'!','@','#','$','%','^','&','*','(',')');
print $list[rand(@list)] foreach (1..10);
更多技術小秘訣請看以下網頁
http://www.geego.com.tw/tech_support/new-tips
@list = (0..9, a..z, A..Z,'!','@','#','$','%','^','&','*','(',')');
print $list[rand(@list)] foreach (1..10);
更多技術小秘訣請看以下網頁
http://www.geego.com.tw/tech_support/new-tips
2011年10月25日 星期二
奇科電腦 技術小秘訣: Perl 程式 嵌入 HTML PHP網頁
技術小秘訣: Perl 程式 嵌入 HTML PHP網頁
因為 HTML 是純文字檔,一般都是用 PHP 或 Perl 來產生 HTML,把 PHP code 放到 Perl 裡面執行或是把 Perl code 放到PHP裡執行,只要在 PHP 裡面呼叫外部的 Perl 程式,或是用 iframe 來另外放 Perl 產生的頁面即可,因為 PHP 要產生網頁比起 Perl 相對上省力得多!
更多技術小秘訣請參考以下網頁
http://www.geego.com.tw/tech_support/new-tips
因為 HTML 是純文字檔,一般都是用 PHP 或 Perl 來產生 HTML,把 PHP code 放到 Perl 裡面執行或是把 Perl code 放到PHP裡執行,只要在 PHP 裡面呼叫外部的 Perl 程式,或是用 iframe 來另外放 Perl 產生的頁面即可,因為 PHP 要產生網頁比起 Perl 相對上省力得多!
更多技術小秘訣請參考以下網頁
http://www.geego.com.tw/tech_support/new-tips
2011年10月19日 星期三
技術小秘訣: Perl 版的密碼產生器
技術小秘訣: Perl 版的密碼產生器
#!/usr/bin/Perl
@list = (0..9, a..z, A..Z,'!','@','#','$','%','^','&','*','(',')');
$range = 71;
for ( 1..10 )
{
$pass .= $list[int(rand($range))];
}
print "$pass\n";
http://www.geego.com.tw/tech_support/tech_tips_07_perl-passwrd-producer
#!/usr/bin/Perl
@list = (0..9, a..z, A..Z,'!','@','#','$','%','^','&','*','(',')');
$range = 71;
for ( 1..10 )
{
$pass .= $list[int(rand($range))];
}
print "$pass\n";
http://www.geego.com.tw/tech_support/tech_tips_07_perl-passwrd-producer
2011年10月9日 星期日
技術小秘訣: Perl模仿指令grep,找出搜尋字串的上五行及下五行?
技術小秘訣: Perl模仿指令grep,找出搜尋字串的上五行及下五行?
#!/usr/bin/perl
open(FILE, "檔案名稱" ) or die "Can't open file\n";
@lines = <FILE>;
@hit_line = map { $lnum++; /關鍵字/?$lnum-1:() } @lines;
foreach $hit_line ( @hit_line )
{
print "\n\nHit line number is : $hit_line+1\n\n";
for( ( $hit_line - 5 ) .. ($hit_line + 5 ) )
{
print "$lines[$_]";
}
}
open(FILE, "檔案名稱" ) or die "Can't open file\n";
@lines = <FILE>;
@hit_line = map { $lnum++; /關鍵字/?$lnum-1:() } @lines;
foreach $hit_line ( @hit_line )
{
print "\n\nHit line number is : $hit_line+1\n\n";
for( ( $hit_line - 5 ) .. ($hit_line + 5 ) )
{
print "$lines[$_]";
}
}
技術小秘訣: 你們知道第一個破解 DVD 的程式就是 Perl 嗎?
技術小秘訣: 你們知道第一個破解 DVD 的程式就是 Perl 嗎?
你們知道第一個破解DVD的程式就是Perl嗎?Perl只需了短短七行程式碼就解開DVD的鎖碼了. Perl的出現,可以讓管理者用非常輕鬆的方式完成工作。同樣一件事若用 C 語言 來撰寫,可能 要幾百行甚至到千行程式碼,但用Perl來寫可能只要幾十行就結束了。
特別提供Perl DVD 破解碼如下:
$_='while(read+STDIN,$_,2048){$a=29;$b=73;$c=142;$t=255;
@t=map{$_%16or$t^=$c^=( $m=(11,10,116,100,11,122,20,100)[$_/16%8])&110;
$t^=(72,@z=(64,72,$a^=12*($_%16 -2?0:$m&17)),$b^=$_%64?12:0,@z)[$_%8]}(16..271);
if((@a=unx"C*",$_)[20]&48){$h =5;$_=unxb24,join"",@b=map{xB8,unxb8,chr($_^$a[--$h+84])}@ARGV;s/...$/1$&/;
$ d=unxV,xb25,$_;$e=256|(ord$b[4])<<9|ord$b[3];
$d=$d8^($f=$t&($d12^$d4^ $d^$d/8))<<17,$e=$e8^($t&($g=($q=$e14&7^$e)^$q*8^$q<<6))<<9,$_=$t[$_]^ (($h=8)+=$f+(~$g&$t))for@a[128..$#a]}print+x"C*",@a}';
s/x/pack+/g;eval
特別提供Perl DVD 破解碼如下:
$_='while(read+STDIN,$_,2048){$a=29;$b=73;$c=142;$t=255;
@t=map{$_%16or$t^=$c^=( $m=(11,10,116,100,11,122,20,100)[$_/16%8])&110;
$t^=(72,@z=(64,72,$a^=12*($_%16 -2?0:$m&17)),$b^=$_%64?12:0,@z)[$_%8]}(16..271);
if((@a=unx"C*",$_)[20]&48){$h =5;$_=unxb24,join"",@b=map{xB8,unxb8,chr($_^$a[--$h+84])}@ARGV;s/...$/1$&/;
$ d=unxV,xb25,$_;$e=256|(ord$b[4])<<9|ord$b[3];
$d=$d8^($f=$t&($d12^$d4^ $d^$d/8))<<17,$e=$e8^($t&($g=($q=$e14&7^$e)^$q*8^$q<<6))<<9,$_=$t[$_]^ (($h=8)+=$f+(~$g&$t))for@a
s/x/pack+/g;eval
2011年10月6日 星期四
2011年9月30日 星期五
2011年9月21日 星期三
奇科電腦 Perl Programming 課程視頻教學(2) : 雜湊取用方式Hash雜湊-建立 Hash方式
奇科電腦 Perl Programming 課程視頻教學(2) : 雜湊取用方式Hash雜湊-建立 Hash方式
訂閱:
文章 (Atom)
