不确定如何使用 expect 脚本处理大块文本
Not sure how to deal with with large blocks of text with expect script
我正在尝试编写一个 expect 脚本来自动执行 WowzaStreamingEngine 安装程序。 运行 安装程序输出一个很长(有些人可能会说长得荒谬)的 EULA,然后需要一些交互 - 接受 EULA,设置用户名和密码,重复密码,以及设置它是否应该从引导。我用 autoexpect 记录了我的步骤,但我有几个问题。首先,它只会在我 运行 自动脚本所在的同一终端中 运行 (我假设它是因为 autoexpect 每次按下 space 栏前进时都会记录特定数量的行脚本通过了长得荒谬的 EULA),但脚本不会完成 - 它到达最后一个问题,然后挂起。这是完整的、未修改的脚本(交互,space 栏除外,从第 1284 行开始):
我尝试删除所有文本块,将脚本减少到只包含需要交互的问题的程度:
但这不会超过 EULA 中的第一个文本块。
我最接近完成脚本的是删除最后一个交互提示后出现的所有内容(我 post 编辑了第三个 link,但我需要更多声誉指向 post 3 links).
这似乎让我回到了用户提示,但它不起作用 - 我必须 cntl-c 才能进入功能用户提示。
如有任何帮助或指导,我们将不胜感激。
有了exp_continue
,您的所有需求都将得到满足。它会导致 expect
再次变为 运行。使用这个,我们可以简化脚本如下,
set timeout 300; # Setting 'timeout' to 5 mins
set prompt "#|>|\$"; # We escaped the `$` symbol with backslash to match literal '$'
set linuxPassword "yourpassword"
spawn /bin/bash; # spawning 'bash' shell here
expect -re $prompt
send "sudo sh WowzaStreamingEngine-4.1.2.deb.bin\r"
expect {
"password for $tcl_platform(user): $" {send "$linuxPassword\r"; exp_continue}
-- "--More--" {send " "; exp_continue}
"\\[yes or no]" {send "yes\r"; exp_continue}
"User Name: $" {send "username\r"; exp_continue}
"Password: $" {send "password\r"; exp_continue}
"enter a Wowza Streaming Engine license key" {send "license-key-here\r"; exp_continue}
-re $prompt {puts "INSTALLATION COMPLETED SUCCESSFULLY"}
timeout {puts "TIMEOUT HAPPENED"}
}
$
符号和 [
对 Tcl
和 Expect
都很重要,这就是为什么它用反斜杠简单转义的原因。 \$
将匹配文字 $
,类似地,\\[
将匹配文字 [
在这里,我们要注意超时。我已将其设置为 5 分钟,在该时间间隔内,我们需要的所有模式都应该匹配。否则,肯定会超时。您可以根据需要更改 timeout
。
注意: 我试图在我的电脑中检查此安装,但我卡在了 'User Name' 部分。提供用户名后,它抛出错误
A password is required. Please try again.
WowzaStreamingEngine-4.1.2.deb.bin: 1242: read: Illegal option -s
因此,我无法在最后验证我的代码。因此,检查并让我知道更改。
与 Dinesh 的答案类似,但您不需要生成 shell,因此更简单:
#!/usr/bin/expect -f
set timeout -1
spawn sudo ./WowzaStreamingEngine-4.1.2.deb.bin
expect {
-gl "*--More--*" { send -- " "; exp_continue }
-ex "Do you agree to the above license terms?"
}
send -- "yes\r"
expect -gl "*User Name: "
send -- "UserName\r"
expect -gl "*Password: "
send -- "password\r"
expect -gl "*Confirm Password: "
send -- "password\r"
expect -gl "*Please enter a Wowza Streaming Engine license key*"
send -- "license key\r"
expect -ex "Start Wowza Streaming Engine automatically when this system reboots?"
send -- "yes\r"
expect eof
修剪自动生成的脚本可能是一门艺术。
我正在尝试编写一个 expect 脚本来自动执行 WowzaStreamingEngine 安装程序。 运行 安装程序输出一个很长(有些人可能会说长得荒谬)的 EULA,然后需要一些交互 - 接受 EULA,设置用户名和密码,重复密码,以及设置它是否应该从引导。我用 autoexpect 记录了我的步骤,但我有几个问题。首先,它只会在我 运行 自动脚本所在的同一终端中 运行 (我假设它是因为 autoexpect 每次按下 space 栏前进时都会记录特定数量的行脚本通过了长得荒谬的 EULA),但脚本不会完成 - 它到达最后一个问题,然后挂起。这是完整的、未修改的脚本(交互,space 栏除外,从第 1284 行开始):
我尝试删除所有文本块,将脚本减少到只包含需要交互的问题的程度:
但这不会超过 EULA 中的第一个文本块。
我最接近完成脚本的是删除最后一个交互提示后出现的所有内容(我 post 编辑了第三个 link,但我需要更多声誉指向 post 3 links).
这似乎让我回到了用户提示,但它不起作用 - 我必须 cntl-c 才能进入功能用户提示。
如有任何帮助或指导,我们将不胜感激。
有了exp_continue
,您的所有需求都将得到满足。它会导致 expect
再次变为 运行。使用这个,我们可以简化脚本如下,
set timeout 300; # Setting 'timeout' to 5 mins
set prompt "#|>|\$"; # We escaped the `$` symbol with backslash to match literal '$'
set linuxPassword "yourpassword"
spawn /bin/bash; # spawning 'bash' shell here
expect -re $prompt
send "sudo sh WowzaStreamingEngine-4.1.2.deb.bin\r"
expect {
"password for $tcl_platform(user): $" {send "$linuxPassword\r"; exp_continue}
-- "--More--" {send " "; exp_continue}
"\\[yes or no]" {send "yes\r"; exp_continue}
"User Name: $" {send "username\r"; exp_continue}
"Password: $" {send "password\r"; exp_continue}
"enter a Wowza Streaming Engine license key" {send "license-key-here\r"; exp_continue}
-re $prompt {puts "INSTALLATION COMPLETED SUCCESSFULLY"}
timeout {puts "TIMEOUT HAPPENED"}
}
$
符号和 [
对 Tcl
和 Expect
都很重要,这就是为什么它用反斜杠简单转义的原因。 \$
将匹配文字 $
,类似地,\\[
将匹配文字 [
在这里,我们要注意超时。我已将其设置为 5 分钟,在该时间间隔内,我们需要的所有模式都应该匹配。否则,肯定会超时。您可以根据需要更改 timeout
。
注意: 我试图在我的电脑中检查此安装,但我卡在了 'User Name' 部分。提供用户名后,它抛出错误
A password is required. Please try again.
WowzaStreamingEngine-4.1.2.deb.bin: 1242: read: Illegal option -s
因此,我无法在最后验证我的代码。因此,检查并让我知道更改。
与 Dinesh 的答案类似,但您不需要生成 shell,因此更简单:
#!/usr/bin/expect -f
set timeout -1
spawn sudo ./WowzaStreamingEngine-4.1.2.deb.bin
expect {
-gl "*--More--*" { send -- " "; exp_continue }
-ex "Do you agree to the above license terms?"
}
send -- "yes\r"
expect -gl "*User Name: "
send -- "UserName\r"
expect -gl "*Password: "
send -- "password\r"
expect -gl "*Confirm Password: "
send -- "password\r"
expect -gl "*Please enter a Wowza Streaming Engine license key*"
send -- "license key\r"
expect -ex "Start Wowza Streaming Engine automatically when this system reboots?"
send -- "yes\r"
expect eof
修剪自动生成的脚本可能是一门艺术。