Question

I am using Windows machine and I have WinSCP installed.

I am writing a script that logs in to the server and downloads file.

I do not want to store account password in the script. Is there anyway I can login to server with some-kind of host-key or private-key or something.

Was it helpful?

Solution

Yes, you can use the public key authentication. But for that you still have to store the private key along with your script. Normally the key is encrypted with a passphrase. To automate the login, you would have to store the passphrase to the script file anyway (using the -passphrase switch). So still, if anyone gets an access to your machine, he/she is still able to steal your identity, just as with the password. Though there's an advantage. You can have multiple keys (while only one password). If you use a special key for the script and the key is ever compromised, you can revoke it, while keeping the other keys.

Note that, if you are not absolutely sure of the physical and electronic security of the system on which you are connecting, there's hardly any way to setup an automatic authentication. If you are sure about the security, storing password in the script file is just ok.

Anyway, your question is mostly duplicate of:
How do I setup Public-Key Authentication?

For WinSCP specifics, see the guide to Setting up SSH public key authentication.

See also the WinSCP guide to Protecting credentials used for automation.

OTHER TIPS

I had a similar issue on windows so I used Putty instead http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

If you need to generate a public key then use: http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe

I gave the public key + password to whoever owned the SFTP server to install it on his side. I saved the private key on my side lest say on "C:\privatekey.ppk" You don't use password on your script but you link to the private which you must have on you machine.

Then, when you want to automate a batch to download from the FTP server the Pageant in order to load the private key into session http://the.earth.li/~sgtatham/putty/latest/x86/pageant.exe

Then use the PSFTP to connect and perform actions http://the.earth.li/~sgtatham/putty/latest/x86/psftp.exe

So here is sample code for the batch file:

!--Loading the key to session--!
@C:\pageant.exe "C:\privatekey.ppk"
!--Calling the PSFTP.exe with the uaser and sftp address + command list file--!
@C:\psftp user@your.server.address -b C:\sftp_cmd.txt

Command list file (sftp_cmd.txt) will like like this:

mget "*.*" !--downloading every thing
!--more commands can follow here
close

Now, all you need to to schedule it in scheduled tasks *I wish it was simple as unix's cron job....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top