Expectコマンドを使用して、JBOSSインストーラー・パラメーターを自動的に渡します。

Expectコマンドを使用して、JBOSSインストーラー・パラメーターを自動的に渡します。

手動で実行するときに入力を要求するJBOSSインストーラがあります。以下は、以下の実行例です。

[sp@sp baseInstaller]$ ./advStart.sh config
Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml

init:

config:
   [groovy] coa.install.props properties
   [groovy]
   [groovy]   ? indicates a response is required from you
   [groovy]   [value] is the current value
   [groovy]        to keep current value just press <enter>
   [groovy]        or type new value and press <enter>
   [groovy]   {value,value..} shows the allowed values
   [groovy] installer version 6.0.3.4
   [groovy]
   [groovy] Jboss Server Config Options (changeable after initial config)
   [groovy] -------------------------------------------------------------
   [groovy] ? host                        [sp.resource.com]
resource.com
   [groovy]     ip=10.50.55.90
   [groovy]   host changed to resource.com
   [groovy] ? bind to host only or all ports (all has security implications)
   [groovy]                               [host]
   [groovy]                                                  {host,all,custom}
all
   [groovy]   bind to host only or all ports (all has security implications) changed to all
   [groovy] ? min memory                  [64]
64

このプロセスを自動化するために期待されるスクリプトを書いてみました。以下は私のスクリプトです。

[sp@sp baseInstaller]$ cat saba.sh
#!/usr/bin/expect


# Accessing command line arguments
set arg1 [lindex $argv 0]
set arg2 [lindex $argv 1]

# Printing the arguments
puts "Argument 1: $arg1"
puts "Argument 2: $arg2"

cd /home/sp/jboss/sp/baseInstaller
spawn ./advStart.sh config

# Expect the prompt for the argument with a timeout of 10 seconds
expect {
        "? host                        [sp.resource.com]" {
        # Send the host value
        send "$arg1\r"
        exp_continue
        }
    "? bind to host only or all ports (all has security implications)" {
        # Send the argument value
        send "$arg2\r"
    }
    timeout {
        puts "Error: Timed out while waiting for the prompt."
        exit 1
    }
    eof
} -timeout 10

# Wait for the script to finish
wait

これで、このスクリプトを実行すると下に到達し、進行しなくなります。この問題を解決するのに役立ちますか?

[sp@sp baseInstaller]$ expect saba.sh resource.com all
Argument 1: resource.com
Argument 2: all
spawn ./advStart.sh config
Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml

init:

config:
   [groovy] coa.install.props properties
   [groovy]
   [groovy]   ? indicates a response is required from you
   [groovy]   [value] is the current value
   [groovy]        to keep current value just press <enter>
   [groovy]        or type new value and press <enter>
   [groovy]   {value,value..} shows the allowed values

ベストアンサー1

expect使用されるフォームには非常に厳格です{}-timeout 10最後にコンテンツを追加すると、{}複数行{}形式が認識されなくなり、一致するパターンが誤って解釈されます。 debug(option)を使用してスクリプトを実行すると、それを確認できます-d

次の簡単な例を考えてみましょう。

expect {
  "abc" exit 1
} -timeout 10

デバッグが表示されます

expect: does "" (spawn_id exp4) match glob pattern "\n  "abc" exit 1\n"? no
"10"? no

ご覧のとおり、パターンは「{」と「}」の間のすべてと見なされ、「-timeout」は一致時に実行されるコマンドと見なされます。 「10」は一致させる次のパターンである。

"? host [sp.resource.com]"また、パターンは基本的に「glob」と仮定され、[文字セットで始まる特殊文字の場合も同様であるため、パターンは一致しません。-ex意味するパターンの前にプレフィックスを付ける必要があります。完全にフィット求める。スペースがデータと正確に一致していることを確認してください。

おすすめ記事