2017-08-03 58 views
2

这里的学生工作者通过ssh存储输出。这是我第一次使用perl,而且我似乎已经用完了解决方案,所以我需要你的帮助!使用Net :: SSH :: Expect

我尝试在HP的StoreOnce 3540命令的输出中存储通过SSH模块的变量,所以我可以提取一些 信息我想监控:

不知道,如果它是有用的,但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 - 我还试图用捕捉::微型模块却又:

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

不打印干啥g当我运行脚本。

6 - 同样的事情会的发球台:

$string = tee {$ssh->send("performance")}; print $string;

不打印任何东西

由于同样的错误如雨后春笋般冒出,甚至当我使用不同的模块,我明白我”米大概 缺少的东西,由于我的语言知识缺乏和我缺乏经验,但我不能 似乎发现这里有什么问题。 $ssh->问题是否存在问题?

谢谢

+0

'send'不返回任何东西所以没有什么可以存储的。你究竟想要回到什么地方,并让你阅读模块上的cpan文档?http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod – scrappedcola

+0

我试图找回系统/ show/performance命令的输出。我想把这个输出放在一个字符串中,用正则表达式提取一些信息(但现在只是把输出放在一个字符串中,就是我所要求的)。 – Ect0plasme

回答

2

Perl将主要收集各种输出而不需要额外的包。只有特殊情况下,你需要额外的东西。在这种情况下,Net :: SSH :: Expect就足够了。

我没有我的系统上的Net :: SSH ::期待但它好像你需要的是:

$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

如果你想保存在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报告

相关问题