使用 Windows 批处理脚本保护 FTP [英] Secure FTP using Windows batch script

查看:57
本文介绍了使用 Windows 批处理脚本保护 FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

echo 用户 ftp_user>ftpcmd文件回显密码>>ftpcmd文件echo put c:directory\%1-export-%date%.csv>>ftpcmd文件回声退出>>ftpcmd文件ftp -n -s:ftpcmd.dat ftp.MyFTPSite.com删除 ftpcmd.dat

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

谢谢.

解决方案

首先确保你理解,如果你需要使用

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

<小时>

对于 FTPS,替换 open 中的 sftp:// 命令 带有 ftpes://(明确TLS/SSL) 或 ftps://(隐式 TLS/SSL).删除 -hostkey 开关.

winscp.com/log=ftpcmd.log/command ^打开 ftps://ftp_user:password@ftp.MyFTPSite.com -explicit"^把 c:directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^出口"

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

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

<小时>

查看完整的ftp.exe到WinSCP的转换指南.

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

<小时>

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

出于这个原因,WinSCP 支持(语言环境中立)时间戳格式.例如 %TIMESTAMP#yyyymmdd% 在任何语言环境中解析为 20170515.

(我是 WinSCP 的作者)

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:

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.

解决方案

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="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-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=""server's 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:

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

这篇关于使用 Windows 批处理脚本保护 FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆