這裏的學生工作者通過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->
問題是否存在問題?
謝謝
'send'不返回任何東西所以沒有什麼可以存儲的。你究竟想要回到什麼地方,並讓你閱讀模塊上的cpan文檔?http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod – scrappedcola
我試圖找回系統/ show/performance命令的輸出。我想把這個輸出放在一個字符串中,用正則表達式提取一些信息(但現在只是把輸出放在一個字符串中,就是我所要求的)。 – Ect0plasme