【问题标题】:Calling connect in void context is deprecated不推荐在 void 上下文中调用 connect
【发布时间】:2017-03-21 02:23:45
【问题描述】:

我正在尝试使用 Net::SSH2 在下面的 Perl 代码中,但我收到此错误。

在 test.pl 第 9 行不推荐在 void 上下文中调用 connect。
Net::SSH2: 无法连接到 xx.xx.xxx.xx:ssh: test.pl 第 9 行的文件描述符错误。

use strict;

use Net::SSH2;
#use Net::SSH2::Expect;

#my $Hostname = 'xx.xx.xx.xx';

my $ssh = Net::SSH2->new();

#$ssh->timeout(10);

$ssh->connect('xx.xx.xx.xx');

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 它要你使用:$ssh->connect('xx.xx.xx.xx') or $ssh->die_with_error;
  • 弃用警告是因为 Net::SSH2 希望您检查 connect 的返回值。它建议做$ssh->connect( ... ) or ssh2->die_with_error。由于您没有这样做,因此它直接dies 本身,并且还会发出警告。 Bad file descriptor 错误是你真正的问题。
  • @simbabque,“错误的文件描述符错误是你真正的问题”,不太确定。我认为打印 void context 警告可能会破坏错误。 OP 应该首先添加 or $ssh->die_with_error 并再次运行程序。此外,在无效的上下文中,它总是说“连接失败”,即使这不是问题。
  • @simbabque,刚刚检查过。 warnif 确实可以打败 $!
  • 我现在不得不修复你的几个帖子。请阅读Markdown help 和可能的Markdown: Syntax 并学习正确使用markdown,

标签: perl ssh


【解决方案1】:

将 connect 方法保留在 if 或使用 die_with_error 帮助我修复了代码。

use strict;

use Net::SSH2;

my $ssh=Net::SSH2->new();

if($ssh->connect('xx.xx.xx.xx'))
  {
     print "successfully connected";

     $ssh->auth_password('username','password') or $ssh->die_with_error;

  }

【讨论】:

  • connect 失败时,您的代码仍然会调用auth_password。这没有任何意义。
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2015-04-30
相关资源
最近更新 更多