I was getting the following error when a script was being executed via cron.

Host key verification failed.
lost connection

But when the script was executed manually from the command line it worked without any issues.

* This worked for me in my particular case. This may not work in every case.

I added the following statements to the script and then ran it from cron.

which scp
which ssh

The output from the two which commands :


What this told me was that the PATH I set in my cron script was forcing the script to use the scp and ssh commands in /usr/bin.

When I ran the same commands from the command line it became clear that it worked without any issues using ssh and scp in /usr/local/bin.

# which ssh;which scp

My fix was to specify which scp and ssh the script should use.