Replace FTP with SFTP

2019-06-09 03:00发布

I am currently FTP'ing file to a Unix box from a Windows server. I want to change the following section of the script and migrate the FTP process to be SFTP.

@echo off
echo user %user%> ftpcmd.dat
echo %password%>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %filetobeuploaded% %filepath%%File%>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat %host%
del ftpcmd.dat
:END

I have PSFTP.EXE installed on my windows directory. Using that can I send the files with SFTP ?

How can I replace line ftp -n -s:ftpcmd.dat %host% to be SFTP. I understand you can do something like this....

CALL PSFTP.EXE someUserName@54.54.54.54 ....

If anybody is familiar, please can you help me complete it. What arguments would I give? user, pw, remotedir, host details are kept in a properties file. I'm looking to use keys instead of password. Can you pass the existing ftpcmd.dat arr to psftp?

3条回答
小情绪 Triste *
2楼-- · 2019-06-09 03:29

I am not an expert on this, so someone may have a better answer, but I would recommend using pscp instead of psftp. I think it is the more appropriate tool.

Also consider setting up ssh keys so that you do not need to send the password each time

pscp can be downloaded from here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

and if you google "pscp tutorial" I'm sure you can find one of many articles that explains how to use it.

查看更多
不美不萌又怎样
3楼-- · 2019-06-09 03:50

You can also use WinSCP. It supports the SFTP and scripting.

See the guide for converting Windows FTP script to WinSCP SFTP script.

The script will look like:

open sftp://%user%:%password%@%host%
put -transfer=binary %filetobeuploaded% %filepath%%File%
exit

You can run the script like (if saved to script.txt):

winscp.com /script=script.txt

Note that (contrary to the ftp.exe), environment variables are resolved in WinSCP script too.


If you want to use a private key file, use:

open sftp://%user%@%host% -privatekey=path\key.ppk

(I'm the author of WinSCP)

查看更多
狗以群分
4楼-- · 2019-06-09 03:51

The thing is that calling 3rd party EXE files from your batch files, waiting for the exit code, verifying the results... it's all but easy.

I would recommend you to take a look at other scriptable solutions, that allow you to run FTP and SFTP file transfer in a self-contained and more friendly environment. One such solution is Syncplify.me FTP Script, you can find additional details here: http://www.syncplify.me/products/ftp-script/

查看更多
登录 后发表回答