簡體   English   中英

Expect腳本如何確定expect_out的內容

[英]How does Expect script determine contents of expect_out

我正在嘗試編寫一個腳本,該腳本將為 vpn 提供憑據,但需要注意的是,VPN 要求我提供 OTP。

這是我的腳本:

#! /usr/bin/expect

set vpn    [ lindex $argv 0]
set group  [ lindex $argv 1]
set user   [ lindex $argv 2]
set secret [ lindex $argv 3]

log_user 2 
spawn nmcli con up $vpn --ask

expect {
        "GROUP:" { 
                send "$group\r"
                exp_continue
        }
        "Username:" { 
                send "$user\r"
                exp_continue
        }
        Password: {
                send_user "\nProvide RSA OTP:"
                expect_user -re ":(.*)\r"
                set otp $expect_out(0,string)  <--------- Error!!!
                set pass "$secret$otp"
                send "$pass\r"
                exp_continue
        }
        "Connection successfully activated" {
                send_user "Connected to VPN\r"
        }
        "Login failed" {
                send_user "Login failed\r"
                exp_continue
        }
        "already active" {
                send_user "Already connected to VPN\r"
        }
}
exit

我包含了一個指向我認為問題根源的箭頭,因為當我運行這個腳本時,它看起來像 expect_out 包含Password: 鑒於我在手冊頁中讀到的內容,我想每次進行新匹配時都會清除緩沖區,因此它包含在最近的 expect 子句中匹配的字符串(在這種情況下, expect_user -re ":(.*)\\r" ),但是看來我錯了。 我沒有看到任何說明需要使用不同的功能訪問 expect_user 內容,就像interact一樣,所以我不確定我的密碼去哪里了?

謝謝

使用send_user您不會看到send_user內容的回顯,這與發送到生成的命令時不同,因此您永遠不會匹配:除非用戶明確鍵入它。 此外,您將閱讀換行符,而不是回車符。 所以用

expect_user -re "(.*)\n"

獲取用戶輸入, $expect_out(1,string) (1 not 0) 將包含輸入。

您觀察到的是您的expect_user超時,這就是變量未更新且仍包含舊匹配的原因。


為了保護自己免受看不見的超時的影響,您可以通過以下方式設置“全局”超時檢查

proc abort { } { send_user "Timeout!" ; exit 2 }
expect_before timeout abort

expect_before是在每個expect之前expect_user ,而且似乎在每個expect_user之前expect_user

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM