通过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",可实现交互式的自动安装。
阅读(2039) | 评论(0) | 转发(0) |