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

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

问题描述

我现在有上在不同位置的CSV文件传送到FTP服务器不同的服​​务器批处理脚本。我的脚本看起来类似于这样:

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?

感谢。

推荐答案

首先,确保你明白,如果你需要使用的安全的FTP (= FTPS,按您的文本)或 SFTP (按标签已使用)。

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

正如你所说,你可以使用 WinSCP赋予。它支持FTPS和SFTP。

As you have suggested, you can use WinSCP. It supports both FTPS and SFTP.

使用WinSCP赋予,您的批处理文件看起来像(对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

和批处理文件:

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


虽然使用WinSCP赋予(尤其是直接在命令行提供命令和 %TIMESTAMP%语法) ,该批处理文件可以简化为:


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"

有关 -hostkey 开关的目的,看到的验证脚本主机密钥。

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

有关FTPS,更换 SFTP:// 中的 打开命令 FTPS:// 。删除 -hostkey 开关。你还需要知道,如果你想使用显式TLS / SSL -explicit 开关),或隐式TLS / SSL -implicit 开关):

For FTPS, replace the sftp:// in the open command with ftps://. Remove -hostkey switch. You also need to know, if you want to use Explicit TLS/SSL (-explicit switch), or Implicit TLS/SSL (-implicit 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"

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

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

请参阅从 Ftp.exe的一个完整的转换指南到WinSCP赋予

您也应该阅读:结果
https://winscp.net/eng/docs/guide_automation

请注意使用%TIMESTAMP#YYYYMMDD%而不是%DATE%:的格式%DATE%变量的值是特定于语言环境。因此,请确保您测试您真正要使用这个脚本在同一区域的脚本。例如在我的捷克区域设置的%DATE%解析为 CT 06. 11. 2014 ,可能是什么问题时,用作文件名的一部分。

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.

为此WinSCP赋予(5.7及更高版本)支持(区域中性)时间戳格式本身。例如%TIMESTAMP#YYYYMMDD%解析为 20141106 上的任何区域。

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

请参阅 https://winscp.net/eng/docs/scripting#timestamp

(我的WinSCP的作者)

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

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