0

PerlExpectモジュールを使用する場合、send コマンドの出力をキャプチャする必要があります。

puts $expect_out(buffer);シェルまたは Tcl で、以前に実行したコマンドをキャプチャするために使用できることを知っています。

Perl で同じことを行うにはどうすればよいですか?

リモートマシンで以下のコマンドを送信しています:

$expect->send("stats\n");

statsいくつかの変数で出力をキャプチャする必要があります。

4

1 に答える 1

1

まず最初に、要求されたデータの出力後に 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 に格納されます。その後、この文字列を処理できます。

もう少しお役に立てれば幸いです。;)

于 2016-06-07T11:03:02.697 に答える