How to ignore or Pass 'Yes' when The authenticity of host can't be established in Expect Shell script during Automation

It's possible to avoid this question and accept all incoming keys automaticatilly by using ssh client option StrictHostKeyChecking set to no (default setting is ask, which results in that question):

ssh -o StrictHostKeyChecking=no "$user@$host"

However, note that it would be hardly any secure, as you're basically accepting connect with everyone who may act as a given host. The only secure way to avoid question is to pre-distribute host public keys to clients, i.e. in form of pre-generated known hosts file, which can be used in some way like that:

ssh \
    -o UserKnownHostsFile=PATH_TO_YOUR_KNOWN_HOSTS_FILE \
    -o StrictHostKeyChecking=yes "$user@$host"

This way you'll avoid the question if the check fails, and ssh will result in non-zero exit status.


This works, and it's especially convenient for docker builds

ssh-keyscan hostname.example.com >> $HOME/.ssh/known_hosts

Make use of exp_continue for this scenario.

#!/usr/bin/expect 
set prompt "#|>|\\\$"
spawn ssh dinesh@myhost
expect {
        #If 'expect' sees '(yes/no )', then it will send 'yes'
        #and continue the 'expect' loop
        "(yes/no)" { send "yes\r";exp_continue}
        #If 'password' seen first, then proceed as such.
        "password"
}
send "root\r"
expect -re $prompt

Reference : Expect