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

标签 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.

编辑2: 这是我的完整代码

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

“print "$out","\n";"上的错误消息行:

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

最佳答案

请显示您的更多代码。 $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。您的用户名和密码不正确。

关于perl - 如何使用 Net::SSH::Perl 运行 SSH 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082542/

相关文章:

java - 找不到类 cygwin,错误的解释器

Perl 切换语句

linux - 逐行读取文件 fork/exec - Perl

linux - 用于远程 SSH 的 Git GUI 客户端的解决方案

web-services - 如何找到我的密码?

mysql - 如何将当前日期与数据库条目相匹配并检索它

git - 如何使用git client访问gitlab docker?

bash - 如何在 bash 脚本中使用远程命令结果分配局部变量?

linux - 从 Linux 通过 SSH 连接到 Mac OS X(使用 XForwarding)时如何运行 Emacs?

mysql - 两个月的 Perl 日期计算