PerlExpect
モジュールを使用する場合、send コマンドの出力をキャプチャする必要があります。
puts $expect_out(buffer);
シェルまたは Tcl で、以前に実行したコマンドをキャプチャするために使用できることを知っています。
Perl で同じことを行うにはどうすればよいですか?
リモートマシンで以下のコマンドを送信しています:
$expect->send("stats\n");
stats
いくつかの変数で出力をキャプチャする必要があります。
まず最初に、要求されたデータの出力後に CLI の最後の行がどのように見えるかを知る必要があります。Expect
定義されたタイムアウト内で特定のパターンを検索できます。sth が見つかった場合。$expect->send($command)
- コマンドを使用すると、以降のすべてをキャプチャできます$exp-before()
。または、コマンドの後にすべてをキャプチャする場合$expect->after()
は、特別なシンボルをチェックせずに使用します。
例を挙げましょう:
$expect->send("$command\n");
#mask the pipe-symbol for later use. Expect expects a valid regex
$command =~ s/\|/\\\|/;
#if a huge amount of data is requested you have to avoid the timeout
$expect->restart_timeout_upon_receive(1);
if(!$expect->expect($timeout, [$command])){ #timeout
die: "Timeout at $command";
}else{
#command found, no timeout
$expect->after();
$expect->restart_timeout_upon_receive(1);
if(!expect->expect($timeout,["#"])){
die "Timeout at $command";
} else{
$data = $expect->before(); #fetch everything before the last expect() call
}
}
return $data;
したがって、コマンドを起動する必要があり、コマンドが起動されることを期待してください。この後、コマンド プロンプトまですべてを取得できます。私の場合は、#
. コマンドと最後のコマンドの間の行は$expect->expect($timeout,["#"]
、単一の文字列として $data に格納されます。その後、この文字列を処理できます。
もう少しお役に立てれば幸いです。;)