Chinaunix首页 | 论坛 | 博客
  • 博客访问: 387706
  • 博文数量: 89
  • 博客积分: 3176
  • 博客等级: 中校
  • 技术积分: 1205
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-15 10:08
文章分类

全部博文(89)

文章存档

2011年(17)

2010年(19)

2009年(35)

2008年(18)

分类: Python/Ruby

2011-04-02 19:30:41


通过cpan安装Net::SSH::Perl


  1. cpan>install Net::SSH::Perl


在安装过程中,本人遇到了一些问题,记录如下


因为cpan对其它软件的依赖性,要求软件版本的不能过低,所以先升级一下这两个模块:
  1. cpan>upgrade Module::Build
  2. cpan>upgrade ExtUtils::Install

Math::BigInt报错:
  1. 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库的开发包。
  1. aptitude install libmath-gmp-perl
  2. aptitude search libgmp3-dev

如前面安装Math:GMP失败过,先把如下文件夹删掉后,再安装吧。
  1. rm -fr /root/.cpan/build/Math-GMP-2.06-*



基本语法

1. 一般语法
  1. my $host = "192.168.14.222";
  2.     my $user = "root";
  3.     my $passwd = "123456";
  4.     my %params = (
  5.     protocal => '2,1',
  6.     debug => '1',
  7.     );
  8.     use Net::SSH::Perl;
  9.     my $ssh = Net::SSH::Perl->new($host[,%params]);
  10.     $ssh->login($user, $pass);
  11.     my($out, $err, $exit) = $ssh->cmd($cmd);
  12.     print "$out\n";

2. 根据提示信息,返回字符, 可实现交互式操作。
  1. $ssh->register_handler("stdout",sub{
  2.         my($channel,$buffer) = @_;
  3.         my $str = $buffer->bytes;
  4.         if($str =~ /(.*)\[Y\/n\/\?\]/i){
  5.               $channel->send_data("y\n")
  6.            }}
  7.     );


3. Term::ReadKey模块   得到一个互动的伪终端
  1. use Term::ReadKey;
  2.     ReadMode('raw');
  3.     $ssh->shell;
  4.     ReadMode('restore');




解决连接远程主机慢的问题

网上说要安装三个东西:
YAML
Math::BigInt
Math::BigInt::GMP

之前已经安装完 YAML 和  Math::BigInt 了,在装完 Math::BigInt::GMP 后测试,在与远程主机的连接过程中,速度明显提升(连接到远程主机后操作时间不变)。



实例

实例1: 登录一台远程主机,完成简单操作。

  1. use strict;
  2.     use warnings;
  3.     use Net::SSH::Perl;
  4.     my $host = "192.168.14.222";
  5.     my $user = "root";
  6.     my $passwd = "123456";
  7.     my %params = (
  8.     protocal => '2,1',
  9.     debug => '1',
  10.     );
  11.     my $ssh = Net::SSH::Perl->new($host,%params);
  12.     $ssh->login($user,$passwd);
  13.     my ($out,$err,$exit) = $ssh->cmd("ifconfig");
  14.     print "$out\n";
  15.     $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

程序:
  1. use strict;
  2.     use warnings;
  3.     use Net::SSH::Perl;
  4.     open HOST_LIST, "Remote_host_list.txt" or die "can`t open this file\n";
  5.     sub MySSH{
  6.     my ($host,$user,$passwd) = split;
  7.     my %params = (
  8.     protocal => '2,1',
  9.     # debug => '1',
  10.     );
  11.     my $ssh = Net::SSH::Perl->new($host,%params);
  12.     $ssh->login($user,$passwd);
  13.     $ssh->register_handler("stdout",sub{
  14.         my($channel,$buffer) = @_;
  15.         my $str = $buffer->bytes;
  16.         if($str =~ /(.*)\[Y\/n\/\?\]/i){
  17.               $channel->send_data("y\n")
  18.           }}
  19.     );
  20.     $ssh->cmd("aptitude install ppp");
  21.     print "\n** $host complete...\n\n";
  22.     };
  23.     while(<HOST_LIST>){
  24.     MySSH
  25.     }

说明:
     根据给出的远程主机列表文件,来逐一完成安装PPP的操作,在实际的安装过程中,会出现询问,是否安装一些依赖包,如安装按y,否则按n

情景如下:
Do you want to continue? [Y/n/?]

用"register_handler",可实现交互式的自动安装。





阅读(2026) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~