Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file for PuTTY/PSFTP file transfer automation

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.

cd C:\Program Files (x86)\PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:\
put log.sh
bye

The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.

like image 785
suresh Avatar asked May 08 '13 11:05

suresh


People also ask

What is the difference between SFTP and Psftp?

PSFTP, the PuTTY SFTP client, is a tool for transferring files securely between computers using an SSH connection. PSFTP differs from PSCP in the following ways: PSCP should work on virtually every SSH server. PSFTP uses the new SFTP protocol, which is a feature of SSH 2 only.

What is Psftp PuTTY?

What is PSFTP? PSFTP is the Secure File Transfer Protocol (SFTP) client of PuTTY – world's most popular free SSH client. It one of the many components of PuTTY and is mainly leveraged for file transfer between computers using SSH connection.

How do I connect to Psftp server?

Connect to a remote serverWith the PSFTP login window open, type open followed by the name of the host you are connecting to (for example, open karst.uits.iu.edu or open mercury.uits.indiana.edu ). If the Store key in cache? prompt appears, type y . At the login prompt, enter your username for the remote server.


1 Answers

You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:

cd "C:\Program Files (x86)\PuTTY"
psftp -b "C:\path\to\script\script.txt"

Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b


EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>@<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:

cd "C:\Program Files (x86)\PuTTY"
psftp -b script.txt <user>@<IP> -pw <PW>

And remove the open, <user> and <PW> lines from your script.txt.

Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw


What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.


If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:

(
    echo cd ...
    echo lcd ...
    echo put log.sh
) | psftp <user>@<IP> -pw <PW>

Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.

like image 166
Martin Prikryl Avatar answered Sep 30 '22 05:09

Martin Prikryl