【问题标题】:Retrieve output of top command using Net::SSH2使用 Net::SSH2 检索 top 命令的输出
【发布时间】: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'。

标签: perl ssh


【解决方案1】:

不要使用“top -n 1”或“top”,而是使用“top -b -n 1”

【讨论】:

    猜你喜欢
    • 2018-03-14
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2022-06-15
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多