1.6 Expectスクリプトの使用

Expectスクリプトは、複数のCLIコマンドをバッチ・モードで自動化するために使用できます。Expectは、Oracle VM CLIなどのSSHベースのアプリケーションとともに使用できる、UNIXのスクリプトおよびテスト用ユーティリティです。Expectスクリプトには、通常、.exp拡張子が付いていますが、任意のファイル名接尾辞を付けることができます。このマニュアルおよびスクリプトの例では、Expectスクリプトのファイル名には.shを使用するか、または拡張子を付けません。Expectスクリプトを実行するには、次の構文を使用します。

expect script

または、Expectスクリプトに実行権限を設定していることを確認し、expectコマンドを明示的に指定せずに、他のシェル・スクリプトの場合と同様に実行します。

1.6.1 Expectスクリプトの前提条件

Expectスクリプトを使用するには、Expectユーティリティをインストールしていることを確認します。Oracle LinuxのYumサーバーを使用してExpectをインストールするには、次のように入力します。

# yum install expect

他のオペレーティング・システムにExpectをインストールするには、次を参照してください。

http://expect.sourceforge.net/

1.6.2 Expectスクリプトの作成

CLIでバッチ・ジョブを実行するには、独自のExpectスクリプトを作成できます。この項では、非常に簡単なExpectスクリプトを作成し、CLIで実行する方法を示しますが、Expectで使用されるプログラミング言語の詳細には踏み込みません。Expectスクリプトの作成の詳細は、次を参照してください。

http://expect.sourceforge.net/

Oracle VM Serverを表示する非常に簡単なExpectスクリプトを例1.1「list_server.exp Expectスクリプト」に示します。

例1.1 list_server.exp Expectスクリプト

#!/usr/bin/expect

## Access CLI
set loginUser "admin"
set loginPassword "password"
set mgmtServerAddress manager_host

## Expect Parameters
set timeout 20
set successMsg "Status: Success"
set failureMsg "Status: Failure"

spawn ssh -l $loginUser $mgmtServerAddress -p 10000
expect_after eof {exit 0}

set timeout 10

##interact with SSH
##expect "yes/no" {send "yes\r"}
expect "password:" {send "$loginPassword\r"}
puts "\n## Starting Generated OVMCLI Script... ##\n"
set timeout 600

expect "OVM> "
send "set OutputMode=Verbose\r"
expect $successMsg {} \
    timeout { puts "\n\nTest Failure: \n\r"; exit} 

expect "OVM> "
  send  "list Server\r"
  expect $successMsg {} \
   timeout { puts "\n\nScript Failure: \n\r"; exit}

Oracle VM Manager環境用にloginUserloginPasswordおよびmgmtServerAddress変数を編集します。

このExpectスクリプト・ファイルを実行するには、次のように、expectコマンドを使用し、その後にExpectスクリプトの場所を入力します。

# expect /myscripts/list_server.exp
spawn ssh -l admin localhost -p 10000
admin@localhost's password:
## Starting Generated OVMCLI Script... ##


OVM> set OutputMode=Verbose
Command: set OutputMode=Verbose
Status: Success
Time: date
OVM> list Server
Command: list Server
Status: Success
Time: date
Data:
  id:00:e0:81:4d:41:01:00:e0:81:4d:40:d6:00:e0:81:4d  name:MyServer1
  id:00:e0:81:4d:40:c6:00:e0:81:4d:40:c7:ff:ff:ff:ff  name:MyServer2
  id:00:e0:81:4d:40:f5:00:e0:81:4d:40:be:00:e0:81:4d  name:MyServer3
OVM>