I've got a standard command which I use from the CL to to share my key with a remote host (Raspberry Pi usually). So, using ssh user pi, I send my id_rsa.pub to 192.168.1.5 like this.
cat ~/.ssh/id_rsa.pub | ssh pi@192.168.1.5 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
And it works fine... Because I clear out the known hosts to be safe (with ssh-keygen -R 192.168.1.5) beforehand, I always get the fingerprint warning which I type "yes" (&enter) and then confirm Pi's password (&enter).
However, I'm trying to write it into an automated script (which is fed just the IP and the password) and I can't get it working. So far, I've been trying to prefix the command with variations of...
printf 'yes\n\PASSWORD\n' | SAME COMMAND AS ABOVE
yes | printf 'PASSWORD\n' | SAME COMMAND AS ABOVE
I assume it's an issue with all the redirection/piping but since I stole that command and understand roughly how it works, I'm not experienced enough to know why it's falling over. Any help would be greatly appreciated!