cpan安裝Net::SSH::Perl中遇到的一些問題
使用cpan安裝Net::SSH::Perl:
cpan>install Net::SSH::Perl
期間遇到了一些問題,記錄在此,以備后閱。
因為cpan對其它軟件的依賴性,要求軟件版本的不能過低,所以先升級一下這兩個模塊:
cpan>upgrade Module::Build
cpan>upgrade ExtUtils::Install
Math::BigInt報錯:
Math::BigInt: couldn't load specified math lib(s), fallback to Math::BigInt::Calc at /usr/lib/perl5/site_perl/5.8.8/Crypt/DH.pm line 6
按照報錯信息,把DH.PM這個文件的第六行做更改:
原為:use Math::BigInt lib => “GMP,Pari”;
更為:use Math::BigInt try => “GMP,Pari”;
在安裝過程中,裝到Math::GMP模塊時,報錯而停止。 大概意思是缺少GMP.c文件,故現(xiàn)在系統(tǒng)下安裝一個GMP庫文件,和一個GMP庫的開發(fā)包。
aptitude install libmath-gmp-perl
aptitude search libgmp3-dev
如前面安裝Math:GMP失敗過,先把如下文件夾刪掉后,再安裝吧。
rm -fr /root/.cpan/build/Math-GMP-2.06-*
基本語法
1. 一般語法
my $host = "192.168.14.222";
my $user = "root";
my $passwd = "123456";
my %params = (
protocal => '2,1',
debug => '1',
);
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host[,%params]);
$ssh->login($user, $pass);
my($out, $err, $exit) = $ssh->cmd($cmd);
print "$out\n";
2. 根據(jù)提示信息,返回字符, 可實現(xiàn)交互式操作。
$ssh->register_handler("stdout",sub{
my($channel,$buffer) = @_;
my $str = $buffer->bytes;
if($str =~ /(.*)\[Y\/n\/\?\]/i){
$channel->send_data("y\n")
}}
);
3. Term::ReadKey模塊 得到一個互動的偽終端
use Term::ReadKey;
ReadMode('raw');
$ssh->shell;
ReadMode('restore');
解決連接遠(yuǎn)程主機慢的問題
網(wǎng)上說要安裝三個東西:
YAML
Math::BigInt
Math::BigInt::GMP
之前已經(jīng)安裝完 YAML 和 Math::BigInt 了,在裝完 Math::BigInt::GMP 后測試,在與遠(yuǎn)程主機的連接過程中,速度明顯提升(連接到遠(yuǎn)程主機后操作時間不變)。
實例
實例1: 登錄一臺遠(yuǎn)程主機,完成簡單操作。
use strict;
use warnings;
use Net::SSH::Perl;
my $host = "192.168.14.222";
my $user = "root";
my $passwd = "123456";
my %params = (
protocal => '2,1',
debug => '1',
);
my $ssh = Net::SSH::Perl->new($host,%params);
$ssh->login($user,$passwd);
my ($out,$err,$exit) = $ssh->cmd("ifconfig");
print "$out\n";
$ssh->cmd("mkdir /home/user;touch /home/user/{1..10}.log");
實例2:通過一個遠(yuǎn)程主機列表,登錄N臺遠(yuǎn)程主機,完成可提問式的互動操作。
遠(yuǎn)程主機列表文件Remoto_host_list.txt文件:
192.168.14.222 root 123456
192.168.14.223 root 123456
程序:
use strict;
use warnings;
use Net::SSH::Perl;
open HOST_LIST, "Remote_host_list.txt" or die "can`t open this file\n";
sub MySSH{
my ($host,$user,$passwd) = split;
my %params = (
protocal => '2,1',
# debug => '1',
);
my $ssh = Net::SSH::Perl->new($host,%params);
$ssh->login($user,$passwd);
$ssh->register_handler("stdout",sub{
my($channel,$buffer) = @_;
my $str = $buffer->bytes;
if($str =~ /(.*)\[Y\/n\/\?\]/i){
$channel->send_data("y\n")
}}
);
$ssh->cmd("aptitude install ppp");
print "\n** $host complete...\n\n";
};
while(<HOST_LIST>){
MySSH
}
說明:
根據(jù)給出的遠(yuǎn)程主機列表文件,來逐一完成安裝PPP的操作,在實際的安裝過程中,會出現(xiàn)詢問,是否安裝一些依賴包,如安裝按y,否則按n
情景如下:
Do you want to continue? [Y/n/?]
用"register_handler",可實現(xiàn)交互式的自動安裝。
相關(guān)文章
perl數(shù)組的多數(shù)字下標(biāo)示例代碼
perl數(shù)組中正常的下標(biāo)運算,想必大家都比較熟悉,這里不作說明。本文想說的是perl數(shù)組下標(biāo)的多數(shù)字取值,可以極大的方便數(shù)組的操作2013-02-02讓apache2以cgi方式運行perl cgi程序的實現(xiàn)方法
讓apache2以cgi方式運行perl cgi程序的方法,供大家學(xué)習(xí)參考2013-02-02perl 讀取所需文件的路徑,然后打開相應(yīng)的文件
perl,讀取所需文件的路徑,然后打開相應(yīng)的文件,并對文件中的DNA序列進行計數(shù),substr函數(shù)對長字符串的片段化處理功能2013-03-03