1

I am trying to use the expect utility to automate a task which requires inputting the password during script execution.

expect -c "set timeout 120; 
spawn /bin/bash /home/user/script.sh; 

# Expect 1 - not expected always
expect \"Are you sure you want to continue connecting (yes/no)?\"; send "yes\r"; 

# Expect 2 - not expected always
expect \"Please type 'yes' or 'no':\"; send \"yes\r\"; 

# Expect 3 - this is required always to input the password
expect \"user@xyz.com's password:\"; send \"password\r\"; expect eof"

From the script above, expect 1 and 2 conditions are not required always. Its only needed when executing the script against new servers. Once the identity of the host is added to known hosts, the first 2 expect conditions wont be prompted by the script. Expect 3 is the only condition to be met in most of the cases.

In a system, where the host identity already exist, the expect script executes the first condition and stays until the timeout is reached and doesn't get to expect 3. I am not sure how to achieve this, but is there a way in expect to break if a prompt is not met.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Ganga
  • 883
  • 12
  • 24
  • 3
    http://stackoverflow.com/q/1538444/1005215 – Nehal J Wani Aug 15 '16 at 03:57
  • 1
    expect is not part of bash, it's not specific to bash, it's not shipped with bash, it doesn't even utilize bash under-the-hood (the language it's based on is TCL). You could run an expect script without having bash installed at all. The bash tag has no place in a question specific to expect. – Charles Duffy Aug 17 '16 at 14:50
  • thanks, the expect command itself is not bash as per the script. script.sh which i want expect to execute is a bash script however which is why i am calling the bash executable – Ganga Aug 22 '16 at 05:34

1 Answers1

3

The expect command has a time out, which I forgot how long, but just a couple of seconds. If not found what you are expecting, time out occurs and the expect command will exit. With that in mind, I have set the timeout to 1 second and seems to work. You might want to adjust it to your system.

#!/usr/bin/env tclsh
package require Expect

spawn /bin/bash /home/user/script.sh 

# Time out (in seconds) for the expect commands
set timeout 1

expect "Are you sure you want to continue connecting (yes/no)? " { send "yes\r" }
expect "Please type 'yes' or 'no':" { send "yes\r" }
expect "user@xyz.com's password:" { send "password\r" }
interact

A couple of notes:

  1. This script is not a bash script. It is a TCL script with Expect package. I recommend doing thing this way as TCL is very capable, yet not too hard to learn.
  2. The timeout value is the the key here. You might even want to set different time-out values before each expect command to tailor your script's response time.
  3. Setting timeout to 120 is too long: Each expect command will have to wait 2 minutes before moving on, at which time, your system might timed out before you can get to the next expect command.

Good luck.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • /home/user/script.sh takes about 60 secs to execute, worst case i wanted expect script to fail, if it goes beyond 120 secs. – Ganga Aug 22 '16 at 05:42