cpan安裝Net::SSH::Perl中遇到的一些問題

來源:互聯網
上載者:User

使用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檔案,故現在系統下安裝一個GMP庫檔案,和一個GMP庫的開發包。

複製代碼 代碼如下: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. 根據提示資訊,返回字元, 可實現互動式操作。

複製代碼 代碼如下:$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');

解決串連遠程主機慢的問題

網上說要安裝三個東西:
YAML
Math::BigInt
Math::BigInt::GMP

之前已經安裝完 YAML 和 Math::BigInt 了,在裝完 Math::BigInt::GMP 後測試,在與遠程主機的串連過程中,速度明顯提升(串連到遠程主機後操作時間不變)。

執行個體

執行個體1: 登入一台遠程主機,完成簡單操作。

複製代碼 代碼如下: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:通過一個遠程主機列表,登入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
}

說明:
根據給出的遠程主機列表檔案,來逐一完成安裝PPP的操作,在實際的安裝過程中,會出現詢問,是否安裝一些依賴包,如安裝按y,否則按n

情景如下:
Do you want to continue? [Y/n/?]
用"register_handler",可實現互動自動安裝。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.