Bash Expect脚本:使用zenity生成后定义新变量
我正在编写一个bash脚本,它使用openconnect将VPN与智能卡或RSA令牌连接起来,使用Zenity终端用户友好型,在调用expect spawn进程之前提示用户输入所需变量。除非RSA令牌失去同步,需要用户输入下一个令牌代码,否则它的工作效果非常好 有人知道如何在启动繁殖过程后成功调用zenity吗?我需要用zenity定义一个新变量$token,并将其应用于正在处理的脚本。因为它正在请求下一个令牌代码,所以我不能在调用spawn进程之前预定义变量。下面是bash脚本的一部分。此外,此脚本在后台运行。用户看不到在终端中运行的脚本Bash Expect脚本:使用zenity生成后定义新变量,bash,variables,expect,zenity,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
}
您不需要使用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
@格伦尼查克曼我想他是这么说的。问题是zenity和expect之间的延迟导致RSA令牌溢出了有效代码。我相信你可以使用expect一次生成多个进程。实际上,我昨天刚开始考虑这样做是为了一些工作,但我没有比这更大的帮助。Glenn也许可以,但假设我是正确的。如果我没有仔细阅读问题,就会出现这种情况……我添加了您提供的定义令牌的行,但似乎没有定义变量。输出为空。我认为这与我在bash中使用expect有关。如果我接受您提供的代码,并在一个简单的expect脚本中运行它,那么令牌似乎得到了定义。但是,我把它放在bash中,它没有。想知道在bash shell中使用expect时,是什么阻止了变量的定义?是的,是bash。我正在使用expect-c。。。代替expect-c'…'。在我做了这个更改之后,我不得不重写我的脚本,因为之前定义的bash变量只有在我将它们设置为env变量时才能被调用。然后,我不得不删除所有不再需要的转义字符。这是一场多么痛苦的狂欢和期待啊!不管怎样,多亏了@glennjackman,它现在运行得很好。