My expect script:

#!/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"


mkdir /opt/dir1
mkdir /opt/dir2
cp /root/file1 /opt/dir1

when I run this script by Ansible, after the end of the questions, script stops and won't run another command mkdir /opt/dir1 etc.


interact is an interactive loop that does not exit unless an escape condition is written into it, similar to the ssh -e escape_char feature except that you need to write appropriate code to support it:

#!/usr/bin/env expect
spawn expect
expect "> "
interact {
  "~" { return }
send "exit\r"
expect eof

which when run:

% ./interact
spawn expect
expect1.1> puts hi         # this was typed under interact
expect1.2> exit            # this is where ~ was pressed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.