2011-05-26 113 views
4

我有一個簡單的perl測試腳本,它使用Net::SSH2,並且我無法使其使用公鑰驗證。Net :: SSH2的密鑰

注意:在閱讀了幾篇回覆之後,我意識到我應該提到我創建了腳本來幫助縮小與使用Net :: SSH2的另一個應用程序時遇到的問題。因此,我不可能切換到替代包,如Net::OpenSSHNet::OpenSSH::Compat::SSH2

注意更新:作爲salvapointed outNet::OpenSSH::Compat::SSH2可以不必修補原來的應用程序中使用。

下面的代碼:Ubuntu上使用ssh-keygen

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 -t rsa 
ssh-copy-id [email protected] 
ssh [email protected] 

上述工作正常 - 我可以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-1libssh2-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軟件包。

UPDATE

正如Daniel Stenberg's reply指出,Ubuntu的10.04包現在已經過時了一點(1.2.2版本,與1.2.8最新的穩定版本相比)。在Ubuntu軟件包的頂部安裝libssh2解決了這個問題。但是,這對我來說很混亂。我如何刪除Ubuntu軟件包,從源代碼安裝libssh2,並仍然構建Net :: SSH2 —如何告訴Net :: SSH2在哪裏可以找到libssh2開發頭文件?

UPDATE 2

salvacame to the rescue again,並顯示如何Net::SSH2安裝者可以用libinclude目錄被配置,使得它可以與源被用於安裝libssh2的。謝謝salva!請注意,代替修補Makefile.PL,可以僅覆蓋Makefile.PL頂部的$lib$inc變量。直到我看到salva的補丁時,我才意識到這一點。

回答

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 
+0

感謝您的答覆。我的測試腳本是幫助縮小與使用Net :: SSH2的另一個應用程序的問題,所以不幸的是,我堅持使用它。該應用程序是開源的,所以我最終可能會提交一個允許使用其中一種替代方案的修補程序。同時,我通過從源代碼安裝來實現它,但是我想解決安裝了Ubuntu軟件包和源代碼包的混亂情況。 – Mike 2011-05-27 07:00:17

+0

感謝您的更新 - 這非常酷。我已經在我的測試腳本上嘗試過了,它完美地工作。它應該很簡單,可以在適當的應用程序中覆蓋Net :: SSH2。更重要的是,這是一個非常簡單的補丁,可以添加到未來的版本中 - 如果可用,應用程序可以使用Net :: OpenSSH :: Compat :: SSH2,如果不可用,可以回退到Net :: SSH2。謝謝。 – Mike 2011-05-27 08:43:39

+0

Net :: OpenSSH :: Compat :: SSH2還不是一個成熟的模塊。不要猶豫,以報告任何錯誤或問題,你可能會發現。 – salva 2011-05-27 09:50:35

1

當您從源碼包安裝libssh2默認情況下將自己安裝在/ usr/local的,但默認包在/ usr安裝哪可能是爲什麼它只能找到「庫存」安裝版本。

不幸的是,它似乎你的Ubuntu有一個相當舊的libssh2版本,因爲它從那以後一直被修復很多。

我建議使用libssh2-devel郵件列表來深入討論libssh2問題。這是一個小而友好的社區。

+0

是的,Ubuntu libssh2軟件包版本是1.2.2,而最新的穩定版本是1.2.8版本。最新的版本確實解決了這個問題,但我想知道如何在沒有安裝Ubuntu軟件包的情況下構建和編譯Net :: SSH2。也就是說,我該如何告訴Net :: SSH2安裝程序在哪裏可以找到開發頭文件? – Mike 2011-05-27 07:10:10

1

我爲Net :: SSH2 Makefile.PL創建了一個patch,它允許從命令行設置libssh2庫和頭文件的位置。

以下是完整的構建過程中使用它libssh2 +網:: SSH2捕獲:

[email protected]:/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] 

[email protected]:/tmp/salva$ tar xzf libssh2-1.2.8.tar.gz 
[email protected]:/tmp/salva$ cd libssh2-1.2.8 
[email protected]:/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 
... 

[email protected]:/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 
... 

[email protected]:/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 
... 

[email protected]:/tmp/salva/libssh2-1.2.8$ cd .. 
[email protected]:/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] 

[email protected]:/tmp/salva$ tar xzf Net-SSH2-0.35.tar.gz 
[email protected]:/tmp/salva$ cd Net-SSH2-0.35 
[email protected]:/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 
[email protected]:/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 
[email protected]:/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 
[email protected]:/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) 
[email protected]:/tmp/salva/Net-SSH2-0.35$ sudo make install 
... 
+0

感謝這個解決方案,它超越了責任的要求!我試過了,但Makefile.PL失敗,出現如下錯誤:加載共享庫時出錯:libssh2.so.1:無法打開共享目標文件:沒有這樣的文件或目錄錯誤結果:'ssh2''奇怪的是,如果我刪除'/ usr/local/libssh2/lib/libssh2.so.1'符號鏈接到'/ usr/local/libssh2/lib/libssh2.so.1.0.1',它可以工作。 – Mike 2011-05-27 12:02:55

+0

Rafael Kitover也發佈了Net :: SSH2的0.36版本,並加入了我的補丁 – salva 2011-05-28 22:30:42