cpan安裝Net::SSH::Perl中遇到的一些問(wèn)題
使用cpan安裝Net::SSH::Perl:
cpan>install Net::SSH::Perl
期間遇到了一些問(wèn)題,記錄在此,以備后閱。
因?yàn)閏pan對(duì)其它軟件的依賴(lài)性,要求軟件版本的不能過(guò)低,所以先升級(jí)一下這兩個(gè)模塊:
cpan>upgrade Module::Build
cpan>upgrade ExtUtils::Install
Math::BigInt報(bào)錯(cuò):
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
按照?qǐng)?bào)錯(cuò)信息,把DH.PM這個(gè)文件的第六行做更改:
原為:use Math::BigInt lib => “GMP,Pari”;
更為:use Math::BigInt try => “GMP,Pari”;
在安裝過(guò)程中,裝到Math::GMP模塊時(shí),報(bào)錯(cuò)而停止。 大概意思是缺少GMP.c文件,故現(xiàn)在系統(tǒng)下安裝一個(gè)GMP庫(kù)文件,和一個(gè)GMP庫(kù)的開(kāi)發(fā)包。
aptitude install libmath-gmp-perl
aptitude search libgmp3-dev
如前面安裝Math:GMP失敗過(guò),先把如下文件夾刪掉后,再安裝吧。
rm -fr /root/.cpan/build/Math-GMP-2.06-*
基本語(yǔ)法
1. 一般語(yǔ)法
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ù)提示信息,返回字符, 可實(shí)現(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模塊 得到一個(gè)互動(dòng)的偽終端
use Term::ReadKey;
ReadMode('raw');
$ssh->shell;
ReadMode('restore');
解決連接遠(yuǎn)程主機(jī)慢的問(wèn)題
網(wǎng)上說(shuō)要安裝三個(gè)東西:
YAML
Math::BigInt
Math::BigInt::GMP
之前已經(jīng)安裝完 YAML 和 Math::BigInt 了,在裝完 Math::BigInt::GMP 后測(cè)試,在與遠(yuǎn)程主機(jī)的連接過(guò)程中,速度明顯提升(連接到遠(yuǎn)程主機(jī)后操作時(shí)間不變)。
實(shí)例
實(shí)例1: 登錄一臺(tái)遠(yuǎn)程主機(jī),完成簡(jiǎ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");
實(shí)例2:通過(guò)一個(gè)遠(yuǎn)程主機(jī)列表,登錄N臺(tái)遠(yuǎn)程主機(jī),完成可提問(wèn)式的互動(dòng)操作。
遠(yuǎn)程主機(jī)列表文件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
}
說(shuō)明:
根據(jù)給出的遠(yuǎn)程主機(jī)列表文件,來(lái)逐一完成安裝PPP的操作,在實(shí)際的安裝過(guò)程中,會(huì)出現(xiàn)詢(xún)問(wèn),是否安裝一些依賴(lài)包,如安裝按y,否則按n
情景如下:
Do you want to continue? [Y/n/?]
用"register_handler",可實(shí)現(xiàn)交互式的自動(dòng)安裝。
相關(guān)文章
cpan安裝Net::SSH::Perl中遇到的一些問(wèn)題
cpan安裝Net::SSH::Perl中遇到的一些問(wèn)題,需要的朋友可以參考下2013-02-02perl數(shù)組的多數(shù)字下標(biāo)示例代碼
perl數(shù)組中正常的下標(biāo)運(yùn)算,想必大家都比較熟悉,這里不作說(shuō)明。本文想說(shuō)的是perl數(shù)組下標(biāo)的多數(shù)字取值,可以極大的方便數(shù)組的操作2013-02-02讓apache2以cgi方式運(yùn)行perl cgi程序的實(shí)現(xiàn)方法
讓apache2以cgi方式運(yùn)行perl cgi程序的方法,供大家學(xué)習(xí)參考2013-02-02Perl實(shí)現(xiàn)高水線算法(解決多值比較問(wèn)題方法)
這篇文章主要介紹了Perl實(shí)現(xiàn)高水線算法(解決多值比較問(wèn)題方法),從本文代碼示例中還可以學(xué)習(xí)到數(shù)組遍歷、函數(shù)寫(xiě)法、函數(shù)調(diào)用等知識(shí),需要的朋友可以參考下2015-06-06perl 讀取所需文件的路徑,然后打開(kāi)相應(yīng)的文件
perl,讀取所需文件的路徑,然后打開(kāi)相應(yīng)的文件,并對(duì)文件中的DNA序列進(jìn)行計(jì)數(shù),substr函數(shù)對(duì)長(zhǎng)字符串的片段化處理功能2013-03-03