4

我有一个使用Net::SSH2的简单 perl 测试脚本,但我无法让它与公钥身份验证一起工作。

注意:在阅读了几个回复后,我意识到我应该提到我创建了脚本来帮助缩小我在使用 Net::SSH2 的另一个应用程序中遇到的问题。因此,我无法切换到其他软件包,例如Net::OpenSSHNet::OpenSSH::Compat::SSH2

注意更新:正如萨尔瓦 指出的那样,无需修补原始应用程序即可使用Net::OpenSSH::Compat::SSH2 。

这是代码:

use strict;
use warnings;
use 5.10.0;

use Net::SSH2;

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

if ( $ssh2->connect('hostname') ) {

    $auth = $ssh2->auth_publickey(
       'username',
       '/home/mike/.ssh/id_rsa.pub',
       '/home/mike/.ssh/id_rsa',
       'password'
    );  
}   

if ($auth && $ssh2->auth_ok) {
    say 'Success';
} else {
    say join ', ', $ssh2->error;
}  

我一直ssh-keygen在 Ubuntu 上使用生成密钥对:

ssh-keygen -t rsa
ssh-copy-id user@server
ssh user@server

以上工作正常 - 我可以通过 SSH 连接到服务器。

当我运行 Perl 代码时,使用受密码保护的密钥时出现以下错误:

-16, LIBSSH2_ERROR_FILE, Unable to initialize private key from file

如果我尝试使用非密码保护的密钥,它可以正常工作。

我安装了以下库:

Net::SSH2         0.35

libssh2-1         1.2.2-1
libssh2-1-dev     1.2.2-1
ssh               1:5.3p1-3ubuntu6
openssh-server    1:5.3p1-3ubuntu6
openssh-client    1:5.3p1-3ubuntu6
openssl           0.9.8k-7ubuntu8.6
libssl-dev        0.9.8k-7ubuntu8.6

情节变厚了

如果我删除libssh2-1and libssh2-1-dev,我会得到一个错误,正如预期的那样:

Can't load '/usr/local/lib/perl/5.10.1/auto/Net/SSH2/SSH2.so'

如果我libssh2从源代码构建,我无法重新安装Net::SSH2,因为它找不到开发头文件。但是,如果我重新安装libssh2-1libssh2-1-dev然后libssh2从源代码构建和安装,它就可以工作。

这是否意味着在 Ubuntu 10.04 上构建 libssh2-1-dev 有问题?如果是这样,我如何libssh2从源安装并正确安装Net::SSH2,而不需要libssh2-1-dev. 我认为从源代码构建正在覆盖或覆盖 Ubuntu 包。

更新

正如Daniel Stenberg 的回复中所指出的,Ubuntu 10.04 软件包现在有点过时(版本 1.2.2,与最新的稳定版本 1.2.8 相比)。从 Ubuntu 软件包的“顶部”源安装 libssh2 解决了这个问题。但是,这对我来说感觉很混乱。如何删除 Ubuntu 软件包,从源代码安装 libssh2,并且仍然构建 Net::SSH2 — 我如何告诉 Net::SSH2 在哪里可以找到 libssh2 开发头文件?

更新 2

salva 再次进行救援,并展示了如何使用和目录配置Net::SSH2安装程序,以便它可以与 libssh2 的源安装一起使用。谢谢萨尔瓦!请注意,可以只覆盖.顶部的和变量而不是修补。直到我查看了萨尔瓦的补丁,我才意识到这一点。libincludeMakefile.PL$lib$incMakefile.PL

4

3 回答 3

1

我已经能够使用 Ubuntu 11.04 libnet-ssh2-perl 包中的 Net::SSH2 使用密码保护密钥成功登录服务器。

无论如何,请考虑使用Net::OpenSSHNet::OpenSSH::Compat::SSH2而不是 Net::SSH2。

更新Net::OpenSSH::Compat::SSH2试图替代 Net::SSH2。无需修补应用程序即可试用:

perl -MNet::OpenSSH::Compat=Net::SSH2 yor_app.pl
于 2011-05-26T22:24:43.310 回答
1

当您从源包安装 libssh2 时,默认情况下它将自己安装在 /usr/local 中,而默认包安装在 /usr 中,这可能是它只找到“库存”安装版本的原因。

不幸的是,您的 Ubuntu 似乎有一个相当旧的 libssh2 版本,因为从那时起它已经修复了很多错误。

我建议使用 libssh2-devel 邮件列表来解决深入的 libssh2 问题。这是一个小而友好的社区。

于 2011-05-27T06:46:10.067 回答
1

我为 Net::SSH2创建了一个补丁Makefile.PL,允许从命令行设置 libssh2 库和头文件的位置。

这是使用它的 libssh2 + Net::SSH2 的完整构建过程的捕获:

salva@leon:/tmp/salva$ wget http://www.libssh2.org/download/libssh2-1.2.8.tar.gz
--2011-05-27 11:21:10--  http://www.libssh2.org/download/libssh2-1.2.8.tar.gz
Resolving www.libssh2.org... 80.67.6.50
Connecting to www.libssh2.org|80.67.6.50|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 637707 (623K) [application/x-gzip]
Saving to: `libssh2-1.2.8.tar.gz'

100%[================================================================================================================================================================================================>] 637,707      525K/s   in 1.2s    

2011-05-27 11:21:14 (525 KB/s) - `libssh2-1.2.8.tar.gz' saved [637707/637707]

salva@leon:/tmp/salva$ tar xzf libssh2-1.2.8.tar.gz 
salva@leon:/tmp/salva$ cd libssh2-1.2.8
salva@leon:/tmp/salva/libssh2-1.2.8$ ./configure --prefix=/usr/local/libssh2
checking whether to enable maintainer-specific portions of Makefiles... no
checking for sed... /bin/sed
checking for a BSD-compatible install... /usr/bin/install -c
...

salva@leon:/tmp/salva/libssh2-1.2.8$ make
Making all in src
make[1]: Entering directory `/tmp/salva/libssh2-1.2.8/src'
make  all-am
make[2]: Entering directory `/tmp/salva/libssh2-1.2.8/src'
if /bin/bash ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H   -I../include -I../src   -g -O2 -MT channel.lo -MD -MP -MF ".deps/channel.Tpo" -c -o channel.lo channel.c; \
    then mv -f ".deps/channel.Tpo" ".deps/channel.Plo"; else rm -f ".deps/channel.Tpo"; exit 1; fi
libtool: compile:  gcc -DHAVE_CONFIG_H -I../include -I../src -g -O2 -MT channel.lo -MD -MP -MF .deps/channel.Tpo -c channel.c  -fPIC -DPIC -o .libs/channel.o
...

salva@leon:/tmp/salva/libssh2-1.2.8$ sudo make install
Making install in src
make[1]: Entering directory `/tmp/salva/libssh2-1.2.8/src'
make[2]: Entering directory `/tmp/salva/libssh2-1.2.8/src'
test -z "/usr/local/libssh2/lib" || mkdir -p -- "/usr/local/libssh2/lib"
 /bin/bash ../libtool --mode=install /usr/bin/install -c  'libssh2.la' '/usr/local/libssh2/lib/libssh2.la'
libtool: install: /usr/bin/install -c .libs/libssh2.so.1.0.1 /usr/local/libssh2/lib/libssh2.so.1.0.1
...

salva@leon:/tmp/salva/libssh2-1.2.8$ cd ..
salva@leon:/tmp/salva$ wget http://search.cpan.org/CPAN/authors/id/R/RK/RKITOVER/Net-SSH2-0.35.tar.gz
--2011-05-27 11:22:56--  http://search.cpan.org/CPAN/authors/id/R/RK/RKITOVER/Net-SSH2-0.35.tar.gz
Resolving search.cpan.org... 207.115.101.144
Connecting to search.cpan.org|207.115.101.144|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://osl.ugr.es/CPAN/authors/id/R/RK/RKITOVER/Net-SSH2-0.35.tar.gz [following]
--2011-05-27 11:22:59--  http://osl.ugr.es/CPAN/authors/id/R/RK/RKITOVER/Net-SSH2-0.35.tar.gz
Resolving osl.ugr.es... 150.214.21.7
Connecting to osl.ugr.es|150.214.21.7|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 92434 (90K) [application/x-gzip]
Saving to: `Net-SSH2-0.35.tar.gz'

100%[================================================================================================================================================================================================>] 92,434       328K/s   in 0.3s    

2011-05-27 11:22:59 (328 KB/s) - `Net-SSH2-0.35.tar.gz' saved [92434/92434]

salva@leon:/tmp/salva$ tar xzf Net-SSH2-0.35.tar.gz 
salva@leon:/tmp/salva$ cd Net-SSH2-0.35
salva@leon:/tmp/salva/Net-SSH2-0.35$ wget -q --no-check-certificate -O - https://github.com/salva/net-ssh2/commit/3c7261f4584137f4240d204731e20f709f1addb1.patch|patch -p1
patching file Makefile.PL
salva@leon:/tmp/salva/Net-SSH2-0.35$ perl Makefile.PL lib=/usr/local/libssh2/lib/ inc=/usr/local/libssh2/include/

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.

Debian:   sudo aptitude install libssh2-1-dev
OpenSUSE: sudo zypper in libssh2-1 libssh2-devel

Checking if your kit is complete...
Looks good
Writing Makefile for Net::SSH2
salva@leon:/tmp/salva/Net-SSH2-0.35$ make
cp lib/Net/SSH2/File.pm blib/lib/Net/SSH2/File.pm
cp lib/Net/SSH2/PublicKey.pm blib/lib/Net/SSH2/PublicKey.pm
cp lib/Net/SSH2/Dir.pm blib/lib/Net/SSH2/Dir.pm
cp lib/Net/SSH2/SFTP.pm blib/lib/Net/SSH2/SFTP.pm
cp lib/Net/SSH2/Listener.pm blib/lib/Net/SSH2/Listener.pm
cp lib/Net/SSH2/Channel.pm blib/lib/Net/SSH2/Channel.pm
cp lib/Net/SSH2.pm blib/lib/Net/SSH2.pm
AutoSplitting blib/lib/Net/SSH2.pm (blib/lib/auto/Net/SSH2)
/usr/bin/perl "-Iinc" /usr/local/share/perl/5.10.1/ExtUtils/xsubpp  -typemap /usr/share/perl/5.10/ExtUtils/typemap -typemap typemap  SSH2.xs > SSH2.xsc && mv SSH2.xsc SSH2.c
cc -c  /usr/local/libssh2/include/ -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g   -DVERSION=\"0.35\" -DXS_VERSION=\"0.35\" -fPIC "-I/usr/lib/perl/5.10/CORE"   SSH2.c
In file included from SSH2.xs:11:0:
ppport.h:3042:0: warning: "PERL_UNUSED_DECL" redefined
/usr/lib/perl/5.10/CORE/perl.h:330:0: note: this is the location of the previous definition
cc: /usr/local/libssh2/include/: linker input file unused because linking not done
Running Mkbootstrap for Net::SSH2 ()
chmod 644 SSH2.bs
rm -f blib/arch/auto/Net/SSH2/SSH2.so
LD_RUN_PATH="/usr/local/libssh2/lib:/lib/x86_64-linux-gnu" cc  -shared -O2 -g -L/usr/local/lib -fstack-protector SSH2.o  -o blib/arch/auto/Net/SSH2/SSH2.so     \
       -L/usr/local/libssh2/lib/ -lssh2 -lz -lssl -lcrypto      \

chmod 755 blib/arch/auto/Net/SSH2/SSH2.so
cp SSH2.bs blib/arch/auto/Net/SSH2/SSH2.bs
chmod 644 blib/arch/auto/Net/SSH2/SSH2.bs
Manifying blib/man3/Net::SSH2::File.3pm
Manifying blib/man3/Net::SSH2::Dir.3pm
Manifying blib/man3/Net::SSH2::PublicKey.3pm
Manifying blib/man3/Net::SSH2::SFTP.3pm
Manifying blib/man3/Net::SSH2::Listener.3pm
Manifying blib/man3/Net::SSH2::Channel.3pm
Manifying blib/man3/Net::SSH2.3pm
salva@leon:/tmp/salva/Net-SSH2-0.35$ ldd ./blib/arch/auto/Net/SSH2/SSH2.so
    linux-vdso.so.1 =>  (0x00007fff20abc000)
    libssh2.so.1 => /usr/local/libssh2/lib/libssh2.so.1 (0x00007f464d52f000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f464d317000)
    libssl.so.0.9.8 => /lib/libssl.so.0.9.8 (0x00007f464d09a000)
    libcrypto.so.0.9.8 => /lib/libcrypto.so.0.9.8 (0x00007f464cd0b000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f464c977000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f464c772000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f464d97e000)
salva@leon:/tmp/salva/Net-SSH2-0.35$ sudo make install
...
于 2011-05-27T09:40:37.347 回答