0

このサイトは初めてなので、ご容赦ください。私は Tcl/Expect スクリプトに取り組んでおり、次のルーター出力の 4 行目の部分を一致させようとしています (2 つの可能な出力が示されています)。通常は IP アドレスが含まれますが、2 番目のサンプルのような文字列が含まれる場合があります。

Routing entry for 10.1.1.0/30
  Known via "static", distance 1, metric 0
  Routing Descriptor Blocks:
  * 10.3.3.1
      Route metric is 0, traffic share count is 1

別の可能な出力:

Routing entry for 10.1.2.0/24
  Known via "static", distance 220, metric 0 (connected)
  Advertised by bgp 1234
  Routing Descriptor Blocks:
  * directly connected, via Null0
      Route metric is 0, traffic share count is 1

正規表現を使用した私の期待文は次のとおりです。

expect -re "Routing Descriptor Blocks:\r\n  \\\* (.*)\r\n" {
        set next_hop $expect_out(1,string)
        puts "\n\n*Next-hop address is: $next_hop*\n"
}

(3 つのバックスラッシュは、Tcl 解析を通過するためのものであり、* は正規表現インタープリターに渡され、リテラル アスタリスクと一致します。)

私の問題は、驚くことではありませんが、これは「貪欲な」一致を行っていることです。貪欲である必要はありません。これが明確にされているデバッグ出力を参照してください。

expect: does "show ip route 10.1.1.0\r\nRouting entry for 10.1.1.0/30\r\n  Known via "static", distance 1, metric 0\r\n  Routing Descriptor Blocks:\r\n  * 10.3.3.1\r\n      Route metric is 0, traffic share count is 1\r\n\r\nRouter>" (spawn_id 4) match regular expression "Routing Descriptor Blocks:\r\n  \* (.*)\r\n"? yes
expect: set expect_out(0,string) "Routing Descriptor Blocks:\r\n  * 10.3.3.1\r\n   Route metric is 0, traffic share count is 1\r\n\r\n"
expect: set expect_out(1,string) "10.3.3.1\r\n      Route metric is 0, traffic share count is 1\r\n"

試合を最初の\r\nで止めてほしい。

したがって、貪欲でない一致の場合、「?」を追加する必要があると考えていたでしょう。次のように:

expect -re "Routing Descriptor Blocks:\r\n  \\\* (.*?)\r\n" {
        set next_hop $expect_out(1,string)
        puts "\n\n*Next-hop address is: $next_hop*\n"
}

問題は、これが機能していないように見えることです。デバッグ出力から次のようになります。

bad regular expression: nested *?+
    while executing
"expect -re "Routing Descriptor Blocks:\r\n  \\\* (.*?)\r\n" {
        set next_hop $expect_out(1,string)
        puts "\n\n*Next-hop address is: $next_hop*\n"
}"
    (file "./test_telnet_to_router.exp" line 23)

あまりにも長い間これを見つめてきたので、助けを求めようと思いました。必要な遅延マッチを取得するために何をする必要があるかについてのアイデアはありますか? この HP-UX サーバーでは、基本的な正規表現だけを使用することに固執していることに注意してください... 拡張正規表現は使用できません。

ありがとう、ジェームズ

4

2 に答える 2

1
于 2013-10-04T11:03:55.613 に答える
1

うわー、それは古いです。もうすぐ20歳。アップグレードできる可能性はありますか?

遅延一致を行う 1 つの方法は、特定の文字ではない貪欲な文字シーケンスを検索することです。これはうまくいくかもしれません

-re "Routing Descriptor Blocks:\r\n  \\\* (\[^\n\]+)\n"

もう 1 つの選択肢は、貪欲な一致を行い、キャプチャされた部分を改行で分割することです。

いずれの場合も、末尾のキャリッジ リターンを手動で削除する必要があります。

于 2013-10-04T10:31:54.830 に答える