Search code examples
bashsshrsyncscp

Terminate scp session if key is invalid, without prompting for password


I have a script to monitor remote status of my servers. For this I run a script on each remote machine, which logs information about uptime, cpu load, Distro version to a log file. The script on my master server, queries their servers by doing an scp of these files like this: scp -i ~/.ssh/id_rsa pingmonitor@IP:~/pingmon.dat ./. I have some machines for which this information is to be queried, and others where the remote servers do not supply this information.

What I want to do is run an scp with the private key on the master server, and if successful, the file is received. If the key has not been added to the remote server, scp should fail, and no more is to be done.

The problem is that if the server has not been setup for ssh access previously (not available in known_hosts), it stalls the script, and prompts: "The authenticity of host can't be established. Are you sure you want to continue connecting (yes/no)?". If yes was entered, it then demands the user password about 3 times.

I tried to remove the yes/no, and the password prompts by piping from a file which contains three newlines, but that does not stop it from asking for a password

How can I automate the script such that if a private key is invalid, it stops the session and returns control to the script, without stalling and asking for a password?

I'm using Debian Wheezy.


Solution

  • Set PasswordAuthentication=no in your ssh_config (Most likely in ~/.ssh/config). In case this doesnt work you can also set NumberOfPasswordPrompts=0 to zero so you wont be prompted for a password.