Convert Multiple Sftp Command Lines to a Single Line Command Line

Convert multiple SFTP command lines to a single line command line

Your question has nothing to do with SFTP/sftp. It's just a generic shell question.

So use any method that a shell allows, for example

(echo command1 && echo command2 && echo command3) | sftp ...

How to execute multiple commands in command prompt using batch file?

You could try this to send an "A" to sftpc:

start cmd /k "echo A | sftpc [SERVERNAME] -pk=1"

I don't have sftpc and am just trying a suggestion to help out...

Updated:

If you have multiple commands to give to sftpc, you may find that one of the following methods works, depending on how sftpc works - I am working blind here - still don't have, or want, sftpc.

start cmd /k "echo A & echo cd somewhere & echo get *.* | sftpc [SERVERNAME] -pk=1"

... change "somewhere" to wherever you want to go to.

Or maybe:

Save the following in a file called ftp.cmd

A
cd somewhere
get *.*

Then try:

start cmd /k "sftpc [SERVERNAME] -pk=1 < ftp.cmd"

Transfer multiple files from SFTP older than yesterday using PSCP

The pscp cannot do that.

All you can do, is to the list all the files using the -ls switch, parse the output to find the old files and generate a download script for the identified files.


Or use an SCP/SFTP client capable of selecting files by their timestamp.

For example with WinSCP SFTP/SCP client, you can use the following batch file (.bat) to download the files older than one day:

winscp.com /log=c:\path\to\winscp.log /command ^
"open sftp://root:password@xx.xxx.xxx.xxx/ -privatekey=""C:\putty\key_pk.ppk"" -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
"get -filemask=<1D /home/user/Folder1/* C:\LocalFolder1\SFTP\" ^
"exit"

References:

  • File masks with size and time constraints
  • Guide to automating file transfers from SFTP/SCP server

(I'm the author of WinSCP)

Controlling psftp in a Windows Batch File

I ended up creating a new batch file from the main one that I then told psftp to use:

echo cd downloads/boxee > psftp.bat
echo put "%1.avi" >> psftp.bat
echo quit >> psftp.bat

psftp frontrow@192.168.1.50 -pw aaa -b psftp.bat

Secure FTP using Windows batch script

First, make sure you understand, if you need to use Secure FTP (=FTPS, as per your text) or SFTP (as per tag you have used).

Neither is supported by Windows command-line ftp.exe. As you have suggested, you can use WinSCP. It supports both FTPS and SFTP.

Using WinSCP, your batch file would look like (for SFTP):

echo open sftp://ftp_user:password@ftp.MyFTPSite.com -hostkey="..." >> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv >> ftpcmd.dat
echo exit >> ftpcmd.dat
winscp.com /script=ftpcmd.dat
del ftpcmd.dat

And the batch file:

winscp.com /log=ftpcmd.log /script=ftpcmd.dat /parameter %1 %date%

Though using all capabilities of WinSCP (particularly providing commands directly on command-line and the %TIMESTAMP% syntax), the batch file simplifies to:

winscp.com /log=ftpcmd.log /command ^
"open sftp://ftp_user:password@ftp.MyFTPSite.com -hostkey=""...""" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"

For the purpose of -hostkey switch, see verifying the host key in script.

Easier than assembling the script/batch file manually is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you:

Generate batch file

All you need to tweak is the source file name (use the %TIMESTAMP% syntax as shown previously) and the path to the log file.


For FTPS, replace the sftp:// in the open command with ftpes:// (explicit TLS/SSL) or ftps:// (implicit TLS/SSL). And remove the -hostkey switch.

winscp.com /log=ftpcmd.log /command ^
"open ftps://ftp_user:password@ftp.MyFTPSite.com -explicit" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"

You may need to add the -certificate switch, if your server's certificate is not issued by a trusted authority.

Again, as with the SFTP, easier is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you.


See a complete conversion guide from ftp.exe to WinSCP.

You should also read the Guide to automating file transfers to FTP server or SFTP server.


Note to using %TIMESTAMP#yyyymmdd% instead of %date%: A format of %date% variable value is locale-specific. So make sure you test the script on the same locale you are actually going to use the script on. For example on my Czech locale the %date% resolves to čt 06. 11. 2014, what might be problematic when used as a part of a file name.

For this reason WinSCP supports (locale-neutral) timestamp formatting natively. For example %TIMESTAMP#yyyymmdd% resolves to 20170515 on any locale.

(I'm the author of WinSCP)

Best way to script remote SSH commands in Batch (Windows)

The -m switch of PuTTY takes a path to a script file as an argument, not a command.

Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m

So you have to save your command (command_run) to a plain text file (e.g. c:\path\command.txt) and pass that to PuTTY:

putty.exe -ssh user@host -pw password -m c:\path\command.txt

Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).

A command-line syntax is identical, an output redirection added:

plink.exe -ssh user@host -pw password -m c:\path\command.txt > output.txt

See Using the command-line connection tool Plink.

And with Plink, you can actually provide the command directly on its command-line:

plink.exe -ssh user@host -pw password command > output.txt

Similar questions:

Automating running command on Linux from Windows using PuTTY

Executing command in Plink from a batch file



Related Topics



Leave a reply



Submit