我不知道如果我設法安裝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更容易使用。
打開調試與我的$的ssh =淨:: SSH :: Perl->新($遠程主機,選項=> [ 「調試是」]);打印前檢查err的值。如果您不確定,請告訴我們關閉err的值和調試輸出。 – weismat 2012-04-10 03:50:28
這是「打印」$ out「,」\ n「;」。在添加了你給我的代碼後,我得到了同樣的錯誤信息(除了不同的行號,但指的是同一行代碼)。 – Sakura 2012-04-10 04:41:30
@weismat:這不是你爲這個模塊啓用調試的方式。 – Borodin 2012-04-10 07:03:04