繁体   English   中英

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

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

我正在使用bash脚本进行工作,该脚本使用openconnect通过Zenity(最终用户友好)将VPN与智能卡或RSA令牌连接,在调用期望的生成过程之前会提示用户输入所需的变量。 除非RSA令牌不同步,否则要求用户输入下一个令牌代码,否则它将非常有效。

有人知道在启动生成过程后如何成功调用zenity吗? 我需要使用zenity定义一个新变量($ token)并将其应用到正在处理的脚本中。 由于它正在请求下一个令牌代码,因此无法在调用生成进程之前预定义变量。 以下是bash脚本的一部分。 另外,此脚本在后台运行。 用户看不到终端中正在运行的脚本。

    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
    }

您不需要使用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]

如果用户单击“确定”,则输入的文本将在$ token中,而$ status将为0。

如果用户单击“取消”或单击Esc,则$ status将为1,并且$ token包含字符串“子进程异常退出”。 大概用户不想继续,因此您可以执行以下操作:

if {$status != 0} exit

暂无
暂无

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

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