使用Net :: SSH :: Expect

时间:2017-08-03 17:42:35

标签: perl ssh

这里的学生工作者。这是我第一次使用perl,我似乎已经没有解决方案,所以我需要你的帮助!

我试图通过ssh模块将一个命令的输出存储在一个变量中的HP Storeonce 3540上,这样我就可以提取一些 我要监控的信息:

不确定它是否有用,但是FYI输出是这样的:

系统/显示>性能

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";

在这里,我想存储"表现"命令的输出。   我尝试了很多组合,所以在这里:

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-> protocole ??

谢谢

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

如果要在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报告中