安装perl Net::SSH2模块

发布时间:2020-11-28编辑:脚本学堂
安装perl Net::SSH2模块

关于此模块的解释:
For a perl implementation that does not require the system ssh command, see Net::SSH::Perl instead.
For a wrapper version that allows you to use passwords, see Net::SSH::Expect instead.
For another non-forking version that uses the libssh2 library, see Net::SSH2.
For a way to execute remote Perl code over an ssh connection see IPC::PerlSSH.

1、
wget http://search.cpan.org/CPAN/authors/id/R/RK/RKITOVER/Net-SSH2-0.24.tar.gz
[root@srv14 Net-SSH2-0.24]# perl Makefile.PL

The libssh2 library is required by this module.  If you don't have it, you can
download it from http://www.libssh2.org; you may also need OpenSSL, which can be
obtained from http://www.openssl.org.

On Debian do:
sudo aptitude install libssh2-1-dev

Can't link/include 'libssh2.h', 'ssh2'

2、
wget http://www.libssh2.org/download/libssh2-1.2.tar.gz
./configure --prefix=/usr/local/libssh2
make
make install
 
ExtUtils-MakeMaker

3、修改Net-SSH2的makefile.pl文件,添加刚才的libssh2的安装目录(如果安装时不指定目录则不需要)
vi Makefile.PL
 

复制代码 代码如下:
my @search_paths = (
  $Config{siteprefixexp}, $Config{prefixexp},
  '/usr', '/usr/local', '/opt','/usr/local/libssh2',
  $ENV{HOME}
);

4、[root@srv12 Net-SSH2-0.24]# perl Makefile.PL
The libssh2 library is required by this module.  If you don't have it, you can
download it from http://www.libssh2.org; you may also need OpenSSL, which can be
obtained from http://www.openssl.org.

On Debian do:
sudo aptitude install libssh2-1-dev

Checking if your kit is complete...
Looks good
Warning: prerequisite ExtUtils::MakeMaker 6.42 not found. We have 6.30.
Writing Makefile for Net::SSH2

ExtUtils::MakeMaker版本低,升级一下吧:
wget http://search.cpan.org/CPAN/authors/id/M/MS/MSCHWERN/ExtUtils-MakeMaker-6.54.tar.gz

[root@srv12 ExtUtils-MakeMaker-6.54]# perl Makefile.PL
Checking if your kit is complete...
Looks good
Using included version of ExtUtils::Manifest (1.56) as it is newer than the installed version (1.46).
Using included version of ExtUtils::Command (1.16) as it is newer than the installed version (1.09).
Using included version of ExtUtils::Installed (1.43) as it is newer than the installed version (0.08).
Using included version of ExtUtils::Packlist (1.43) as it is newer than the installed version (0.04).
Using included version of ExtUtils::Install (1.52) as it is newer than the installed version (1.33).
Writing Makefile for ExtUtils::MakeMaker

make
make install

OK,安装Net::SSH2
[root@srv12 Net-SSH2-0.24]# perl Makefile.PL

The libssh2 library is required by this module.  If you don't have it, you can
download it from http://www.libssh2.org; you may also need OpenSSL, which can be
obtained from http://www.openssl.org.

On Debian do:
sudo aptitude install libssh2-1-dev

Writing Makefile for Net::SSH2
其实似乎不升级ExtUtils::MakeMaker也可以,因为不升级也可以生成Makefile文件。

关于上面的提示:仅仅是提示而已,不管你是不是装了libssh,openssl,它都会提示,看一下makefile.pl就知道了。

现在可以make,make install了。

另外,如果在make时:
-L/usr/local/lib -L/usr/lib -lssh2 -lz -lssl -lcrypto       
/usr/bin/ld: cannot find -lssl
collect2: ld returned 1 exit status
make: *** [blib/arch/auto/Net/SSH2/SSH2.so] Error 1

需要安装openssl-devel包。

安装完成,测试
 

复制代码 代码如下:

#!/usr/bin/perl -w
use Net::SSH2;

my $ssh2 = Net::SSH2->new();

$ssh2->connect('118.219.3.105') or die $!;

         if ($ssh2->auth_password('root','test@test.com')) {
             my $chan = $ssh2->channel();
             $chan->exec('date >/tmp/123');
}


注:exec的输出不在perl脚本的执行端