2017-08-03 76 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報告