简体   繁体   English

Expect脚本:使用zenity生成后定义新变量

[英]Expect Script: Define new variable after spawn with zenity

I am working on a bash script that uses openconnect to connect a VPN with a smartcard or RSA token using Zenity (end user friendly) which prompts users for the required variable(s) before calling the expect spawn process. 我正在使用bash脚本进行工作,该脚本使用openconnect通过Zenity(最终用户友好)将VPN与智能卡或RSA令牌连接,在调用期望的生成过程之前会提示用户输入所需的变量。 It works great unless RSA token gets out of sync, requiring the user to enter the next tokencode. 除非RSA令牌不同步,否则要求用户输入下一个令牌代码,否则它将非常有效。

Does anyone know how to successfully call zenity after starting the spawn process? 有人知道在启动生成过程后如何成功调用zenity吗? I need to define a new variable ($token) with zenity and apply it to expect script that is in process. 我需要使用zenity定义一个新变量($ token)并将其应用到正在处理的脚本中。 Since it is requesting the next token code, I can't predefine the variable before calling the spawn process. 由于它正在请求下一个令牌代码,因此无法在调用生成进程之前预定义变量。 Below is part of the bash script. 以下是bash脚本的一部分。 Also, this script runs in the background. 另外,此脚本在后台运行。 The user does not see the script running in a terminal. 用户看不到终端中正在运行的脚本。

    function rsa() {
    while [ -z "$user" ]; do
      user
      if [[ $? -eq 1 ]]; then
        exit 1
      fi
    done
    while [ -z "$pin" ]; do
      pin
      if [[ $? -eq 1 ]]; then
        exit 1
      elif [ -n "$pin" ]; then
        notify-send -t 10000 "Starting VPN" "Attempting connection to the network."
        expect -c "
          spawn sudo openconnect https://***removed*** -g ***removed*** -u $user --no-dtls --no-cert-check --no-xmlpost 
          expect {
            \"Failed to obtain WebVPN cookie\" {
              puts [exec notify-send -t 10000 \"Connection Unsuccessful\" \"Connection attempt halted.\"]
              exit
              }
            \"Password:\" {
              send $pin\r
              expect {
                \"TOKENCODE:\" {
                  ***need to call zenity and define $token here***
                  send $token\r
                  interact
                  } 
                \"Login failed\" {
                  puts [exec notify-send -t 10000 \"Incorrect PIN\" \"Connection attempt halted.\"]
                  exit
                  } 
                \"Failed to obtain WebVPN cookie\" {
                  puts [exec notify-send -t 10000 \"Connection Unsuccessful\" \"Connection attempt halted.\"]
                  exit
                  }
                \"Connected tun0\" {
                  puts [exec notify-send -t 10000 \"Connection Successful\" \"VPN Connected\"]
                  interact
                  }
                }
              }
            }"
    fi
    done
    exit
    }

You don't need to use spawn : since expect is built on Tcl, you can use exec to call zenity: 您不需要使用spawn :由于Expect是基于Tcl构建的,因此可以使用exec调用zenity:

# the quoting for the --text option looks funny, but Tcl requires that
# the quote appear at the start of a word
# (otherwise a quote is just a regular character)
set status [catch {exec zenity --entry "--text=Enter the current RSA token"} token]

If the user clicked OK, the entered text will be in $token and $status will be 0. 如果用户单击“确定”,则输入的文本将在$ token中,而$ status将为0。

If the user clicked Cancel or hit Esc, then the $status will be 1 and $token holds the string "child process exited abnormally". 如果用户单击“取消”或单击Esc,则$ status将为1,并且$ token包含字符串“子进程异常退出”。 Presumably, the user does not want to continue, so you can do: 大概用户不想继续,因此您可以执行以下操作:

if {$status != 0} exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM