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

时间:2015-03-26 19:46:05

标签: bash variables expect zenity

我正在开发一个bash脚本,它使用openconnect将VPN与智能卡或RSA令牌连接起来使用Zenity(最终用户友好),在调用expect spawn进程之前提示用户输入所需的变量。除非RSA令牌不同步,否则它会很有效,需要用户输入下一个令牌代码。

有没有人知道如何在启动生成过程后成功调用zenity?我需要使用zenity定义一个新变量($ token)并将其应用于期望正在处理的脚本。由于它正在请求下一个令牌代码,因此在调用spawn进程之前,我无法预定义变量。下面是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
    }

1 个答案:

答案 0 :(得分:0)

您不需要使用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
相关问题