This is a short summary what you need to avoid any type of interaction when accessing a machine by SSH.
- Known hosts entry is missing.
- Known hosts entry is incorrect.
- Public key is incorrect or missing.
- Keyboard Authentication is enabled when public key failed.
- stdin is connected and the remote command waits for input.
Here is what you need to do to circumvent everything:
- Ensure to use the correct public key (if necessary pass it using -i)
- Ensure to pass "-o UserKnownHostsFile=/dev/null" to avoid termination when the known hosts key has changed (Note: this is highly insecure when used for untrusted machines! But it might make sense in setups without correctly maintained known_hosts)
- Ensure to pass "-o StrictHostKeyChecking=no" to avoid SSH complaining about missing known host keys (caused by using /dev/null as input).
- Pass "-o PreferredAuthentications=publickey" to avoid password querying when the public key doesn't work
- Pass "-n" to avoid remote interaction
Example command line:
ssh -i my_priv_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PreferredAuthentications=publickey user@host -n "/bin/ls"