期待される出力で単語を確認し、テキストファイルに追加します。

期待される出力で単語を確認し、テキストファイルに追加します。

私はこのコードを持っています:

#!/opt/tools/unsupported/expect-5.39/bin/expect

set timeout 10
match_max 256
log_file report.txt

expect_after eof {exit 0}
spawn ssh -l user ip

expect "(yes/no)?" { send "yes\r" }
expect "password:" { send "password\r" }

expect "~]#" { send "date\r" }

set timeout 600

expect "~]#" { send "scp user@ip:/sometlink/AMM.tar.gz /somelink/\r" }

expect "Password:" { send "password\r" }
sleep 5

set timeout 3600

expect "~]#" { send "swadm install import AMM\r" }
sleep 5

expect "~]#" { send "swadm install apply AMM\r" }

expect "~]#" { send "swadm install show AMM\r"}

expect -re "(.*)\r\nproduct-state=(.*)"

foreach line [split $expect_out(1,string) \n] {
        if {[string match *Applied* $line]} {
            send_log "Product install of AMM ----------------------------- Passed"
                    }
    else {
           send_log "Product install of AMM ----------------------------- Failed"
            }
}


expect "~]#" { send "date\r" }
expect "~]#" { send "exit\r" }

swadm install show AMMの出力を確認する必要があります。

product-id=AMM
product-name=Application Manager
product-version=1.0.0
product-state=Applied
product-description=
NCL-list=AMMLV010

「app」という単語が見つかった場合は、インストールが成功したことを示すテストファイルに追加され、見つからない場合はインストールの失敗を示します。しかし、他の部分ではエラーが発生します。

invalid command name "else"
     while executing
"else {
           send_log "Product install of AMM -----------------------------     Failed"

("foreach" body line 6)
invoked from within
"foreach line [split $expect_out(1,string) \n] {
    if {[string match *Applied* $line]} {
            send_log "Product install of AMM-----..."

ベストアンサー1

私はそうします:

expect "~]#" { send "swadm install show AMM\r"}
expect "~]#" {
    if {[string match {*product-state=Applied*} $expect_out(buffer)]} {
        do-thing-1
    } else {
        do-thing-2
    }
}

「show」コマンドの後、次のプロンプトが表示されるまで待ちます。その後、すべての期待されるものが含まれ、expect_out(buffer)バッファテキスト全体に対して文字列一致(または必要に応じて正規表現一致)を使用できます。行ごとに分割する必要はありません。

おすすめ記事