Expectを使用したUnixシェルスクリプト

Expectを使用したUnixシェルスクリプト

ちょうど期待を使い始めました。サーバーからトークンを取得するために、次のコードを実行しています。

set timeout 20

set token ""

sleep 1

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

sleep 1


expect {
    -indices -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

このコードはほとんどのスイッチで正常に動作しますが、一部のスイッチではRE006コードのため失敗します。したがって、これらのスイッチではスイッチの種類を変更する必要があります。次のように変更しました。

set timeout 60

set token ""

sleep 1

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

sleep 1

expect {
    -re "RE006"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype1 -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

expect {
    -indices -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

これで、これは以前に失敗したスイッチに対してうまく機能します。この2つの状況をどのように処理する必要がありますか?

ベストアンサー1

これは推測であり、追加情報が必要です。通常、プロンプトを見つけるコマンドを使用してビルドしますが、プロンプトがどのように見えるかわからないため、実行中であると推定されるOPプログラムを調整しました。

主な変更点はRE006を探し、表示されている場合は目的のコマンドを出力してからexp_continueを実行して、現在予想されているステートメントを繰り返すことです。これは通常、タイムアウトの短いRE006を探し、タイムアウト時に何もしないよりも優れています。

set timeout 60

set token ""

spawn ssh -l $serveruser1 $serverip

# -- is used to disable the interpretion of flags by Expect.
expect {
    -re "OK"        {
        send_user "** Enter command to get the token. **\n"
        send -- "-t $switchtype -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
    }
    timeout {
        send_user "** Session timeout on $serverip upon LOGIN**\n"
        exit -1
    }
}

# If we get a RE006 code in response to sending "-t $switchtype" then
# send a "-t $switchtype1".
# We then look for at least two lines of output, the first being non-empty
# and the second having a "@" character in it and at least one character before
# the "@". If there is more that one "@" on the second line all the text up
# to the last "@" inclusive will be returned as the token.

expect {
    "RE006"        {
        send_user "Got RE006 response, switching to alternative switchtype\n"
        send -- "-t $switchtype1 -s h -c \[Long_CLLI:$switchname\] -u $switchuser -l auto-sarm -e n \r"
        exp_continue}
    -re "\n.+\n(.+\@)" {
        set token $expect_out(1,string)
        send_user "**Get the token $token **\n"}
    timeout {
        send_user "** Session timeout upon getting token**"
        exit -1}
}

おすすめ記事