【问题标题】:Expect Script: Define new variable after spawn with zenity期望脚本:使用 zenity 生成后定义新变量
【发布时间】:2015-03-26 19:46:05
【问题描述】:

我正在编写一个 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
    }

【问题讨论】:

  • @glennjackman 我想他就是这么说的。问题是 zenity 和期望之间的延迟导致 RSA 令牌翻转有效代码。
  • 我相信你可以通过 expect 一次产生多个进程(实际上我昨天刚开始考虑这样做是为了工作),但我没有比这更有帮助的了。格伦也许可以(假设我是对的)。
  • 当我不仔细阅读问题时会发生这种情况......

标签: bash variables expect zenity


【解决方案1】:

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

【讨论】:

  • 我添加了您提供的用于定义令牌的行,但似乎没有定义变量。输出只是空白。
  • 我认为这与我在 bash 中使用 expect 的事实有关。如果我采用您提供的代码并在一个简单的期望脚本中运行它,那么似乎确实定义了令牌。但是,我把它放在 bash 中,它没有。想知道当在 bash shell 中使用 expect 时,是什么阻止了变量的定义?
  • 是的,它是 bash。我正在使用 expect -c "..." 代替 expect -c '...'。进行此更改后,我不得不重写我的脚本,因为之前定义的 bash 变量只有在我将它们设置为 env 变量时才能被调用。然后,我必须删除所有不再需要的转义字符。多么痛苦(混合 bash 和期望)!无论如何,感谢@glennjackman,它现在运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多