使用Net::SSH::Expect通过ssh存储输出

3

我是一位学生工作者。这是我第一次使用perl,但我已经无法解决问题了,所以需要您的帮助!

我正在尝试通过ssh模块将HP Storeonce 3540命令的输出存储到一个变量中,以便提取我想要监视的一些信息:

不确定是否有用,但FYI,输出如下:

system/show> performance

Service Set 1
 Storage Usage
   Current: xxxxx TB
   Maximum: xxxxx TB
 Throughput
   VTL Read: 0 MB/s
   VTL Write: 0 MB/s
   NAS Read: 0 MB/s
   NAS Write: 0 MB/s
   Catalyst Read: 0 MB/s
   Catalyst Write: 0 MB/s
 Replication
   Inbound: 0 MB/s
   Outbound: 0 MB/s
 Catalyst
   Inbound: 0 MB/s
   Outbound: 0 MB/s

这是代码:

use Net::SSH::Expect;
use Capture::Tiny ':all';
use Backticks;
( ... )
  my $ssh = Net::SSH::Expect->new (
          host => $hp_host,
          password=> $hp_pwd,
          user => $hp_user,
          raw_pty => 1,
          timeout => 20
         );
  my $login_output = $ssh->login();
  if ($login_output !~ />/)
   {
    die "Login has failed. Login output was $login_output";
   }
  $ssh->send("system");
  $ssh->waitfor('\>\s*\z', 5) or die "Error #1-system";
  $ssh->send("show");
  $ssh->waitfor('\>\s*\z', 5) or die "Error #2-show";

在这里,我想要存储“performance”命令的输出。我尝试了很多组合,下面是一些例子:
1- say '$ssh->send("performance")'->stdout; 得到以下错误: String found where operator expected at script_hpstoreonce.pl line 67, near "say '$ssh->send("performance")'" (Do you need to predeclare say?) syntax error at script_hpstoreonce.pl line xx, near "say '$ssh->send("performance")'" 2- Backticks->run( '$ssh->send("performance")' ); print $stdout; 没有打印输出。
3- 每次在命令前加上 "$ssh-> " 时,都会出现以下错误:
例如: $ssh->Backticks->run( 'send("performance")' ); 或者 $string = $ssh->tee('performance'); 错误信息: Can't locate object method "Backticks"/or("Tee")/ via package "Net::SSH::Expect" at script_hpstoreonce.pl line xx. 4- $test = Backticks->stdout('performance'); print $stdout; 或者 print $test; 错误信息: Can't locate object method "Backticks" via package "Net::SSH::Expect" at script_hpstoreonce.pl line xx. 5- 我也尝试使用 Capture::Tiny 模块,但同样出现错误: $stdout = capture { $ssh->send("performance") }; print $stdout; 错误信息: Can't locate object method "capture" via package "Net::SSH::Expect" at script_hpstoreonce.pl line xx. 但是: ($stdout, $stderr) = capture {$ssh->send("performance")}; print $stdout; 或者 $stdout = capture_stdout{$ssh->exec("performance")}; print $stdout; 或者 my($stdout, $stderr, $exit) = $ssh->send('performance'); print $stdout; 或者 $stdout = $ssh->capture('performance'); print $stdout; 当我运行脚本时,没有打印任何内容。
6- 同样的问题也出现在 Tee 中: $string = tee {$ssh->send("performance")}; print $string; 没有打印输出。

由于即使我使用不同的模块,仍然出现相同的错误,我明白这可能是由于我对语言的了解和经验不足而导致的遗漏,但我似乎无法找到问题出在哪里。问题出现在$ssh->协议中吗?

谢谢


send不返回任何内容,所以没有什么可以存储的。你到底想要得到什么,并且你是否阅读了关于该模块的CPAN文档?http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod - scrappedcola
我正在尝试获取系统/显示/性能命令的输出。我想将该输出放入一个字符串中,以便使用正则表达式提取一些信息(但目前只是要求将输出放入一个字符串中)。 - Ect0plasme
2个回答

2

Perl通常可以收集各种输出而不需要额外的软件包。只有在特殊情况下才需要额外的东西。在这种情况下,只需要使用Net::SSH::Expect即可。

然而,我的系统上没有安装Net::SSH::Expect,但似乎您需要的是:

$ssh->send("find /");   # using send() instead of exec()
my $line;
while ( defined ($line = $ssh->read_line()) ) {
  print $line . "\n";  
}

对于你所展示的这么简短的内容,我倾向于跳过send和read_line的复杂性,直接使用exec。

my $who = $ssh->exec("who");
print ($who);

0
如果您想将命令的输出保存在一个报告中,请尝试以下方法。
use Net::SSH::Expect
use strict;
use warnings;

my $stdout = $ssh->exec($command);
my $stdout2 = $ssh->exec($command);

my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "$stdout" . "$stdout2";
close $fh;
print "done\n";

这应该将两个变量打印到一个 .txt 报告中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接