我的期望脚本:
#!/usr/bin/expect -f
set timeout 1
spawn /opt/install.sh
expect "\[input\] Are you installing the application at the central data center? \[yes/no default: yes\]? \[yes\]\r"
send "yes\r"
expect "\[input\] What is the code of central data center \[default: 01\]? \[01\]\r"
send "01\r"
expect "What is ip or hostname of your server \[default: localhost\]? \[localhost\]\r"
send "portal\r"
interact
mkdir /opt/dir1
mkdir /opt/dir2
cp /root/file1 /opt/dir1
当我通过 Ansible 运行此脚本时,在问题结束后,脚本会停止并且不会运行另一个命令mkdir /opt/dir1
等。
答案1
interact
是一个交互式循环,除非写入转义条件,否则不会退出,与该ssh -e escape_char
功能类似,只是您需要编写适当的代码来支持它:
#!/usr/bin/env expect
spawn expect
expect "> "
interact {
"~" { return }
}
send "exit\r"
expect eof
运行时:
% ./interact
spawn expect
expect1.1> puts hi # this was typed under interact
hi
expect1.2> exit # this is where ~ was pressed
%