I have this small script for connecting to new machines I don't have ssh-key to:

function my_ssh () {

    ssh -o "BatchMode yes" user_name@$ip exit > /dev/null 2>&1

    if [ $? -gt 0 ]; then
        echo "1st time connection - adding key to authorized keys list"
        sshpass -p "secret_password" ssh-copy-id user_name@$ip

    echo $optional_cmd
    ssh -X user_name@$ip $optional_cmd

This works weel for servers that either have my ssh-key, or only ask for a password in order to use ssh-copy-id. However, some servers require a "yes/no" after the following question:

The authenticity of host 'A.B.C.D ()' can't be established. ECDSA key fingerprint is SHA256:****. Are you sure you want to continue connecting (yes/no)?

How can I add a "yes" string echoed into the server question stdin and then use the sshpass?

ps, I went over the sshpass code from github but it doesn't seem sshpass is designed for something like this. I think I can modify it to suite my needs, but I prefer using a normal Linux mechanism if possible

  • 2
    use ssh-copy-id -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ... – pynexj Feb 18 '19 at 12:57
  • cool! that works, but if that option wouldn't have worked (because of a tweaked ssh server) would it still be possible to mimic this behaviour? – CIsForCookies Feb 18 '19 at 13:01
  • that's ssh client side options. would not be impacted by ssh server side conf. – pynexj Feb 18 '19 at 13:02
  • 1
    @pynexj You should expand that comment into an actual answer :) – Byte Commander Feb 18 '19 at 13:07
  • 1
    then you need to use utils like expect or sexpect (Expect for Shells). – pynexj Feb 18 '19 at 13:11



for i in $(cat list.txt); do timeout 1 ssh -o StrictHostKeyChecking=no user@$i; done
for i in $(cat list.txt); do ssh-copy-id -i user@$i; done

ssh user@vm1.example.com -- no pass
| improve this answer | |

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.