Monday, 19 August 2013

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

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 queries, 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?

No comments:

Post a Comment