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.