【发布时间】:2017-03-24 14:30:49
【问题描述】:
我正在尝试使用 Net::SSH2 Perl 模块检索 top 命令的输出,但每次都得到空输出。
代码
use Net::SSH2;
use strict;
my $ssh = Net::SSH2->new();
$ssh->connect('xx.xx.xx.xx');
$ssh->auth(username => 'xxxxxx', password => 'xxxxx');
my $channel = $ssh->channel() or do { print" [LOG ERROR]: Failed to create channel. Exiting ...\n"};
$channel->blocking(0);
$channel->shell() ;
sleep(5);
my $cmd='top -n 1';
print $channel "$cmd\n";
sleep(5);
while(<$channel>)
{
print $_;
}
$channel->close();
$ssh->disconnect();
有人可以帮忙吗?
【问题讨论】:
-
使用
exec而不是shell。不要禁用blocking模式。 -
@Salva :我找到了解决此问题的方法。我们可以使用 'top -b -n 1' 代替 'top -n 1'。