Using WinSCP at the command line to automate file transfer

May
19

Few know that WinSCP can be used at command line to automate file transfer. This article discusses the methodology to do so from a windows machine to a Linux/Solaris server running SSH server. You would need the following tools at the Windows client before you go ahead:

puTTYgen.exe
WinSCP.exe

Configuring password-less login into the SSH Server

This step involves configuring certain parameters on both the client end and the server end.

At the Cient End:

Use Puttygen to create a public/private key pair

Once the key has been generated, you’ll see the following window

puttygen2.JPG

Save the private key in a secure location using the respective button. While doing so, you’ll encounter the following message-box

puttygen3.JPG

While it is good idea to protect the keys using a passphrase, it would hinder complete automation of the process. Hence, for the said situation, it is recommended not to protect the keys using a passphrase.
After having saved the keys in a secure location, copy the public key shown in the highlighted area in the below figure into a notepad and save the notepad as public_key.txt

puttygen4.JPG

At the Server end

Login into the SSH server using the credentials that would be used for automated login. E.g. if automation is to be performed for the username “backup1”, you should take care that you login into the server using “backup1” and not any other user (including “root”)
Verify if the directory named .ssh already exists in the home directory. In the above example the following directory should exist:
/home/backup1/.ssh
Verify if the permissions of the directory is 700. This can be verified by using the command

ls –ld /home/backup1/.ssh

You should ideally see the following output:

putty1.JPG

If the permissions do not match, change them using the following command:

chmod 700 /home/backup1/.ssh

Create a file authorized_keys in the .ssh directory. The file should thus be located at /home/backup1/.ssh/authorized_keys
Edit the file authorized_keys to add the contents of public_key.txt saved in the previous section of this SOP. Save the file and exit.

Use the following command to verify that the permissions on the authorized_keys file is 600:

ls –l /home/backup1/.ssh/authorized_keys

If not, change the permissions using

chmod 600 /home/backup1/.ssh/authorized_keys

Open WinSCP.exe and save a new session after having entered the requisite credentials:

winscp1.JPG

Note that the previously saved private key has been used in this configuration.
Set the remote directory in the Environment -> Directories section of the configuration (This assumes that the folder named “testfolder” already exists in the remote SSH server).

winscp2.JPG

Save the session with a unique name (saved as backup_lab in this example)

winscp3.JPG

Now, upon double-clicking session name (backup_lab in this case), the SSH session to the destination server should start. Note that if the entire configuration is correct you should not be prompted for a password. Proceed with the next steps only if the password-less login configuration explained above has worked successfully.

Using WinSCP on the command prompt

The syntax while using winSCP at the command prompt is as follows:

winscp.exe [session-name] /upload file1 [file2] [file3] /defaults

For the above case the command to use would be

winscp.exe backup_lab /upload test.txt /defaults

where we are trying to upload test.txt from the current directory to the folder already specified in the session configuration in WinSCP (/home/backup1/testfolder in this case).
Once the file has been successfully transferred, you would encounter the following success message-box

winscp4.JPG

 And BINGO! you're done!

More info about the command line options for WinSCP can be found here

 

2 comments

yay!

Excellant article...
thanx a lot :)

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.