【发布时间】:2013-09-26 00:01:40
【问题描述】:
似乎从<$chan> 读取会给出空列表,并且它主要发生在延迟较高的网络上的主机上。有没有更健壮的方式与远程主机交互?
use Net::SSH2;
# my $ssh = Net::SSH2->new();
# ...
my $chan = $ssh->channel() or die "no channel\n";
$chan->blocking(1); # even worse with $chan->blocking(0);
$chan->shell();
print $chan "ps -ef\n";
print <$chan>;
编辑:
使用Net::SSH::Any时出错,
无法在 lib/Net/SSH/Any/Backend/Net_SSH2.pm 第 133 行的未定义值上调用方法“exec”。
sub _capture {
my ($any, $opts, $cmd) = @_;
my $ssh2 = $any->{be_ssh2} or return;
my $channel = $ssh2->channel;
my ($out_fh, $err_fh) = __parse_fh_opts($any, $opts, $channel) or return;
$out_fh and die 'Internal error: $out_fh is not undef';
# vvvvvvvvvvvvvv
$channel->exec($cmd); # <--- LINE 133
# ^^^^^^^^^^^^^^
(__io3($any, $ssh2, $channel, $opts->{stdin_data}, undef, $err_fh || \*STDERR))[0];
}
【问题讨论】:
-
通过 Net::SSH2::Channel 对象与远程 shell 对话是相当困难的。尝试改用Net::SSH::Any。
-
@salva 我正在考虑切换到您的
Net::OpenSSH模块。我猜capture()和pipe_out()效果更好? -
它们更易于使用,使用Net::SSH2,您必须自己在低级原语之上编写它们。 Net::OpenSSH 唯一真正的问题是它不能在 Windows 上运行。
-
@salva 是的,我看到它在 win32 上不起作用。顺便说一句,
Net::SSH2::Simple看起来很有趣。 -
查看 Net::SSH2::Simple 源代码我发现它有很多错误。例如,它不处理错误,超时也不起作用。