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:
This step involves configuring certain parameters on both the client end and the server end.
Use Puttygen to create a public/private key pair
Once the key has been generated, you’ll see the following window
Save the private key in a secure location using the respective button. While doing so, you’ll encounter the following message-box
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
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:
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:
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).
Save the session with a unique name (saved as backup_lab in this example)
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.
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
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