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

Perl 使用Net::ssh::Expect通过ssh存储输出,perl,ssh,Perl,Ssh,这里是学生工。这是我第一次使用perl,我的解决方案似乎已经用完了,所以我需要您的帮助 我试图通过ssh模块将命令的输出存储在HP Storeonce 3540的变量中,以便提取一些 我要监视的信息: 不确定它是否有用,但仅供参考,输出如下: 系统/显示>性能 Service Set 1 Storage Usage Current: xxxxx TB Maximum: xxxxx TB Throughput VTL Read: 0 MB/s VTL Write: 0

这里是学生工。这是我第一次使用perl,我的解决方案似乎已经用完了,所以我需要您的帮助

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

不确定它是否有用,但仅供参考,输出如下:

系统/显示>性能

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

获取此错误:

在script_hpstoreonce.pl第67行的“say'$ssh->send(“performance”)”附近找到运算符所需的字符串
(你需要预先声明吗?)
script_hpstoreonce.pl第xx行“say'$ssh->send(“performance”)”附近出现语法错误。

2-
Backticks->run('$ssh->send(“performance”);
打印$stdout

它不打印输出

3- 每次我将
“$ssh->”
放在命令前面时,都会出现以下错误:

例:
$ssh->Backticks->run('send(“performance”)
$string=$ssh->tee('performance')

错误:
无法通过script\u hpstoreonce.pl第xx行的包“Net::SSH::Expect”找到对象方法“Backticks”/or(“Tee”)/。

4-
$test=Backticks->stdout('performance');
打印$stdout
print$test

错误:
无法通过script\u hpstoreonce.pl第xx行的包“Net::SSH::Expect”找到对象方法“Backticks”。

5- 我还尝试使用Capture::Tiny模块,但还是:

$stdout=capture{$ssh->send(“performance”)};
打印$stdout

获取此错误:

无法通过script\u hpstoreonce.pl第xx行的包“Net::SSH::Expect”找到对象方法“capture”。

但是

($stdout,$stderr)=捕获{$ssh->send(“性能”);
打印$stdout

$stdout=capture\u stdout{$ssh->exec(“performance”)};
打印$stdout

my($stdout、$stderr、$exit)=$ssh->send('performance');
打印$stdout

$stdout=$ssh->capture('performance');
打印$stdout

运行脚本时不打印任何内容

6- 同样的道理也适用于T恤:

$string=tee{$ssh->send(“performance”);
打印$string

没有打印任何东西

因为即使在使用不同的模块时,也会出现相同的错误,所以我知道我可能 由于缺乏语言知识和经验,我错过了一些东西,但我不能 似乎发现了这里的问题。
$ssh->
协议中是否存在问题


谢谢

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";  
}
对于您所展示的简短内容,我倾向于跳过发送和读取线路的复杂性,只使用exec

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

如果要在1报告中保存命令的输出,请尝试以下操作

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报告中

send
不会返回任何内容,因此没有任何内容可存储。你到底想取回什么?你读过模块上的cpan文档了吗?我想取回system/show/performance命令的输出。我想把输出放在一个字符串中,用正则表达式提取一些信息(但现在我只是想把输出放在一个字符串中)。