Use private key in WinSCP SCP script

Stan Stewart picture Stan Stewart · Oct 29, 2014 · Viewed 9.4k times · Source

I have been trying to figure this out. I hope someone can help. I have written a batch script to SCP files from a Unix server and store them in a Windows server. I have a key established. However the server is still asking me for a "password". Here is my script below, I am hoping someone can see what I am doing wrong with my options? The key public and private have been set. I was able to login with putty using the key. However trying to use the script it is still asking me for the password. It is like the server does not see the fingerprint.

winscp.com /command "option batch abort" "option confirm off"
"open scp://user@host/ -hostkey=""ssh-rsa fingerprint"""
"get /var/log/CPbackup/backups/R77 backup file* D:\fwlogbackups\*"

Answer

Martin Prikryl picture Martin Prikryl · Oct 30, 2014

You have not specified a private key in the open command. Use the -privatekey= switch for that.

You got probably confused by the -hostkey= switch. That is used to verify a server's public key.
See Verifying the host key.

In case you have specified your account's public key fingerprint in the -hostkey=, you will need to update it to server's public key fingerprint.
See Where do I get SSH host key fingerprint for use with scripting or .NET assembly?

See also Understanding SSH key pairs.


Ntb, you are missing quotes around a path with spaces in the get command.
It should be like:

"get ""/var/log/CPbackup/backups/R77 backup file*"" D:\fwlogbackups\*"

See the command-line syntax.

The complete command-line should be like:

winscp.com /command ^
    "open scp://user@host/ -privatekey=""key.ppk"" -hostkey=""ssh-rsa fingerprint""" ^
    "get ""/var/log/CPbackup/backups/R77 backup file*"" D:\fwlogbackups\*" ^
    "exit"

(The option commands are not needed anymore in the recent versions of WinSCP).


WinSCP GUI can generate a correct script template for you.