使用 Windows 批处理脚本保护 FTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16150152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:27:14  来源:igfitidea点击:

Secure FTP using Windows batch script

windowsbatch-fileftpsftp

提问by etm124

I currently have batch scripts on different servers that transfer a csv file to an FTP server at a different location. My script looks similar to this:

我目前在不同的服务器上有批处理脚本,可以将 csv 文件传输到不同位置的 FTP 服务器。我的脚本看起来类似于:

echo user ftp_user> ftpcmd.dat
echo password>> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.MyFTPSite.com
del ftpcmd.dat

If I wanted to require a secure transmission, is how would my script be updated?

如果我想要求安全传输,我的脚本将如何更新?

Thanks.

谢谢。

回答by Martin Prikryl

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).

首先,请确保您了解是否需要使用安全 FTP(=FTPS,根据您的文本)或SFTP(根据您使用的标签)。

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

Windows command-line 都不支持ftp.exe。正如您所建议的,您可以使用WinSCP。它同时支持 FTPS 和 SFTP。

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

使用 WinSCP,您的批处理文件将如下所示(对于 SFTP):

echo open sftp://ftp_user:[email protected] -hostkey="server's 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-lineand the %TIMESTAMP%syntax), the batch file simplifies to:

尽管使用了 WinSCP 的所有功能(特别是直接在命令行%TIMESTAMP%语法上提供命令),批处理文件简化为:

winscp.com /log=ftpcmd.log /command ^
    "open sftp://ftp_user:[email protected] -hostkey=""server's hostkey""" ^
    "put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
    "exit"

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

关于-hostkey切换的目的,请参见在脚本中验证主机密钥

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:

比手动组装脚本/批处理文件更简单的是在 WinSCP GUI 中设置和测试连接设置,然后让它为您生成脚本或批处理文件

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.

您需要调整的只是源文件名(使用%TIMESTAMP%前面显示的语法)和日志文件的路径。



For FTPS, replace the sftp://in the opencommandwith ftpes://(explicit TLS/SSL) or ftps://(implicit TLS/SSL). Remove the -hostkeyswitch.

对于FTPS,更换sftp://open命令ftpes://明确的TLS / SSL)或ftps://隐式TLS / SSL)。拆下-hostkey开关。

winscp.com /log=ftpcmd.log /command ^
    "open ftps://ftp_user:[email protected] -explicit" ^
    "put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
    "exit"

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

-certificate如果您的服务器证书不是由受信任的机构颁发,您可能需要添加开关。

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.

同样,与 SFTP 一样,在 WinSCP GUI 中设置和测试连接设置更容易,然后让它为您生成脚本或批处理文件



See a complete conversion guide from ftp.exeto WinSCP.

请参阅ftp.exeWinSCP的完整转换指南

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

您还应该阅读自动将文件传输到 FTP 服务器或 SFTP 服务器的指南



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.

使用%TIMESTAMP#yyyymmdd%代替的注意事项%date%%date%变量值的格式是特定于语言环境的。因此,请确保在实际要使用脚本的同一语言环境中测试脚本。例如,在我的捷克语语言环境中,%date%解析为?t 06. 11. 2014,当用作文件名的一部分时可能会出现问题。

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

出于这个原因,WinSCP 原生支持(区域设置中性)时间戳格式。例如在任何语言环境中%TIMESTAMP#yyyymmdd%解析为20170515

(I'm the author of WinSCP)

(我是 WinSCP 的作者)

回答by Matt Williamson

The built in FTP command doesn't have a facility for security. Use cUrlinstead. It's scriptable, far more robust and has FTP security.

内置的 FTP 命令没有安全设施。请改用cUrl。它是可编写脚本的,更加健壮并且具有 FTP 安全性。

回答by Ram

    ftps -a -z -e:on -pfxfile:"S-PID.p12" -pfxpwfile:"S-PID.p12.pwd" -user:<S-PID number> -s:script <RemoteServerName> 2121

S-PID.p12 => certificate file name ;
S-PID.p12.pwd => certificate password file name ; 
RemoteServerName =>  abcd123 ; 
2121 => port number ; 
ftps => command is part of ftps client software ;