如何使用Net :: SSH :: Perl运行SSH命令?

时间:2012-04-10 02:51:46

标签: perl ssh

我不知道我是否成功安装了Net::SSH::Perl模块,但我似乎无法运行以下代码:

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";

&#34;打印&#34; $ out&#34;,&#34; \ n&#34;;&#34;上的错误消息行:

<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更容易使用。

3 个答案:

答案 0 :(得分: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。您的用户名和密码不正确。

答案 1 :(得分:1)

Net :: SSH :: Perl 支持通过用户名/密码登录,我有一个有效的示例,我刚刚使用了它。我从上面使用了代码,取出了双引号(“”)并改用了单引号('')。当出现问题时,“ debug => 1”可用于调试代码。如果设置了debug选项,当您尝试登录时,它将向您显示信息。

我正在连接到基于Windows Powershell的Win32-OpenSSH SSHD服务器,该服务器与具有SFTP支持的BSDLinux SSHD服务器非常相似。支持相同的基于Linux样式的连接。

我整天都在尝试所有其他SSH模块。希望有人可以使用此代码来运行命令并根据需要获取输出。

您可以使用“ cpan -i module-name”安装Net :: SSH :: Perl

 use Net::SSH::Perl;

my $host = 'testmachine.acme.local';     #Or just IP Address
my $user = 'domain\username';            #Or just username
my $pass = 'Password@123';
my $cmd = 'dir C:\\Windows\\'; 

use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host, debug => 1);
$ssh->login($user, $pass);
my ($out, $err, $exit) = $ssh->cmd($cmd);
print "$out", "\n";

答案 2 :(得分:0)

Net :: SSH :: Perl不支持仅通过交互式密码输入或公钥通过用户名/密码登录。有关更多信息,请参阅此文章。

http://www.perlmonks.org/?node_id=590452

相关问题