Search code examples
batch-filesftpputty

Execute sftp commands on remote server using batch file and PuTTY


I have to transfer a file from one server to another. I log in to the first one with PuTTY and then type this:

sftp -v -oIdentityFile=path username@host 
cd path
put file

Everything works perfectly! Now I'm trying to do it with a batch file. In the .bat I have:

putty.exe -ssh host1 -l username1 -pw password1 -m script.txt

In the script.txt file:

sftp -v -oIdentityFile=path username2@host2
cd path
put file
exit

It connects to the server number two but then it stops. The prefix sftp> does not appear and it does not read the following lines. Do you have any suggestion?


Solution

  • The remote shell takes the commands and executes them one by one. So it executes the sftp, waits for it to exit (what it never does) and only then it would execute the cd command (but in shell, not in the sftp), the put (failing as that's not a shell command), etc.


    If your intention was to simulate typing the commands as on a terminal, use Plink and input redirection.

    The Plink (PuTTY command-line connection tool) is a tool from PuTTY package that works like PuTTY, but it is a console, not GUI, application. As such it can use input/output redirection. And anyway, the Plink is the tool to automate tasks, not PuTTY.

    plink.exe -ssh host1 -l username1 -pw password1 < script.txt
    

    For more details, see How to type commands in PuTTY by creating batch file? on Super User.