使用expect的Unix Shell脚本

使用expect的Unix Shell脚本

我刚开始使用expect。我正在运行以下代码以从服务器获取令牌:

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}
}

现在,这对于之前失败的交换机来说效果很好。我该如何处理这两种情况?

答案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}
}

相关内容