2012-04-10 94 views
1

我不知道如果我設法安裝Net::SSH::Perl模塊成功,但我不能似乎能夠運行下面的代碼:如何使用Net :: SSH :: Perl運行SSH命令?

my $ssh = Net::SSH::Perl->new($remote_host); 
$ssh->login($username, $password); 
print "login done", "\n"; 
my ($out, $err, $exit) = $ssh->cmd($cmd); 
print "$out", "\n"; 

我能夠登錄,但無法打印$out。我一直得到這個錯誤:

Use of uninitialized value $out in string at test_ssh.pl line 28. 

第28行是指print "$out", "\n";

我在Cygwin上運行這段代碼。我現在應該怎麼做?

編輯: 我有以下錯誤味精當我跑my $ssh = Net::SSH::Perl->new($remote_host, options => ["Debug yes"]);

Use of uninitialized value $out in string at test_ssh.pl line 29 (#1) 
    (W uninitialized) An undefined value was used as if it were already 
    defined. It was interpreted as a "" or a 0, but maybe it was a mistake. 
    To suppress this warning assign a defined value to your variables. 

    To help you figure out what was undefined, perl will try to tell you the 
    name of the variable (if any) that was undefined. In some cases it cannot 
    do this, so it also tells you what operation you used the undefined value 
    in. Note, however, that perl optimizes your program and the operation 
    displayed in the warning may not necessarily appear literally in your 
    program. For example, "that $foo" is usually optimized into "that " 
    . $foo, and the warning will refer to the concatenation (.) operator, 
    even though there is no . in your program. 

EDIT2: 這裏是我的全部代碼

use strict; 
use warnings; 
use Net::SSH::Perl; 

my $remote_host = '<host ip address>'; 
my $password = 'pass'; 
my $username = 'user'; 
my $cmd   = 'copy run tftp:<my own ip address>'; 

warn "Starting SSH Services:..."; 
my $ssh = Net::SSH::Perl->new($remote_host, debug => 1); 
print "done", "\n"; 

warn "Starting Login:..."; 
$ssh->login($username, $password); 
print "login done", "\n"; 

warn "Starting command:..."; 
#$ssh->cmd($cmd); 
#my($stdout, $stderr, $exit) = $ssh->cmd($cmd); 
my ($out, $err, $exit) = $ssh->cmd($cmd); 
print "$out", "\n"; 

上的「打印 「$了」 錯誤消息, 「\ n」;」 line:

<Computername>: channel 1: new [client-session] 
<Computername>: Requesting channel_open for channel 1. 
<Computername>: Entering interactive session. 
<Computername>: Channel open failure: 1: reason 4: 
Use of uninitialized value $out in string at test_ssh.pl line 29. 

最後編輯:我決定使用Net :: Appliance :: Session通過SSH登錄到網絡設備。它比Net :: SSH :: Perl更容易使用。

+0

打開調試與我的$的ssh =淨:: SSH :: Perl->新($遠程主機,選項=> [ 「調試是」]);打印前檢查err的值。如果您不確定,請告訴我們關閉err的值和調試輸出。 – weismat 2012-04-10 03:50:28

+0

這是「打印」$ out「,」\ n「;」。在添加了你給我的代碼後,我得到了同樣的錯誤信息(除了不同的行號,但指的是同一行代碼)。 – Sakura 2012-04-10 04:41:30

+0

@weismat:這不是你爲這個模塊啓用調試的方式。 – Borodin 2012-04-10 07:03:04

回答

1

請顯示更多代碼。 $cmd的價值是多少?

請注意,login方法不會執行登錄:它只存儲爲每個cmd呼叫建立連接時要使用的用戶名和密碼。

使調試信息的正確方法是

my $ssh = Net::SSH::Perl->new($remote_host, debug => 1); 

這會給你跟蹤從cmd方法,應該說信息,除其他事項外

Sending command: xxx 
Entering interactive session. 

,應該給你一些線索出了什麼問題。


您的調試輸出顯示問題。查看SSH2.h,開放失敗原因4是SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED。您的用戶名和密碼不正確。

+0

我在「EDIT2」下添加了完整的代碼。 – Sakura 2012-04-10 06:12:37

+0

謝謝。我已經添加到我的答案來解釋你的錯誤。通話結束後'$ err'和'$ exit'的內容是什麼? – Borodin 2012-04-10 07:00:56

+0

嗨。我很確定用戶名和密碼是正確的,因爲我使用了相同的證書並使用Net :: SSH和Expect編碼登錄,並且不會給我任何錯誤。事實上,當我成功登錄系統時,它設法打印出歡迎消息。 – Sakura 2012-04-10 07:04:59