从Windows批处理文件上传文件到FTP时,获取“无法打开到xxxx端口的数据连接”

时间:2022-10-04 02:04:37

I am trying to upload a text file to FTP server using a batch file. It logins successfully and shows

我正在尝试使用批处理文件将一个文本文件上载到FTP服务器。它成功登录并显示。

Port command sent successfully

港口命令发送成功

but after that shows

但在显示

Could not open data connection to port xxxx
Connection timed out

无法打开到端口xxxx的数据连接超时

This is the batch script:

这是批脚本:

@echo off

for %%A in (*.csv) do set latest=%%A 
echo Latest file is %latest%  

echo MYUSERNAME> upload.txt 
echo MYPASSWORD>> upload.txt 
echo asc>>upload.txt 
echo put %latest% s.txt>> upload.txt 
echo quit >> upload.txt  

ftp -s:upload.txt server58.hostinger.in

1 个解决方案

#1


5  

This looks like a typical problem with an FTP active mode. The server cannot connect back to your machine to establish a data transfer connection.

这看起来像是FTP活动模式的典型问题。服务器无法连接回您的机器以建立数据传输连接。

That typically happens as nowadays most client machines are behind a firewall or NAT or both, what prevents the FTP active mode from working. To make the active mode working you need to open your firewall (not recommended) and/or configure NAT routing rules.

这通常发生在现在大多数客户端机器都位于防火墙或NAT或两者的后面,这阻止了FTP活动模式的工作。要使活动模式工作,您需要打开防火墙(不推荐)和/或配置NAT路由规则。

See my article on FTP modes and configuring network for an active mode.

请参阅我关于FTP模式和为活动模式配置网络的文章。


Or you use a passive FTP mode. The Windows ftp.exe client does not support the passive mode though, what makes it pretty useless nowadays.

或者使用被动FTP模式。Windows ftp。但是exe客户端不支持被动模式,这使得它现在变得非常无用。

So you need to use another command-line FTP client. A majority of FTP clients do support the passive mode.

因此,您需要使用另一个命令行FTP客户端。大多数FTP客户端都支持被动模式。

For example with WinSCP your batch file would be like:

例如,对于WinSCP,您的批处理文件如下:

@echo off

for %%A in (*.csv) do set latest=%%A 
echo Latest file is %latest%

winscp.com /command ^
    "open ftp://MYUSERNAME:MYPASSWORD@server58.hostinger.in/" ^
    "put -transfer=ascii %latest% s.txt" ^
    "exit"

Note that WinSCP defaults to the passive mode.

注意,WinSCP默认为被动模式。

For details see WinSCP guides for:

详情请参阅WinSCP指南:

(I'm the author of WinSCP)

(我是WinSCP的作者)

#1


5  

This looks like a typical problem with an FTP active mode. The server cannot connect back to your machine to establish a data transfer connection.

这看起来像是FTP活动模式的典型问题。服务器无法连接回您的机器以建立数据传输连接。

That typically happens as nowadays most client machines are behind a firewall or NAT or both, what prevents the FTP active mode from working. To make the active mode working you need to open your firewall (not recommended) and/or configure NAT routing rules.

这通常发生在现在大多数客户端机器都位于防火墙或NAT或两者的后面,这阻止了FTP活动模式的工作。要使活动模式工作,您需要打开防火墙(不推荐)和/或配置NAT路由规则。

See my article on FTP modes and configuring network for an active mode.

请参阅我关于FTP模式和为活动模式配置网络的文章。


Or you use a passive FTP mode. The Windows ftp.exe client does not support the passive mode though, what makes it pretty useless nowadays.

或者使用被动FTP模式。Windows ftp。但是exe客户端不支持被动模式,这使得它现在变得非常无用。

So you need to use another command-line FTP client. A majority of FTP clients do support the passive mode.

因此,您需要使用另一个命令行FTP客户端。大多数FTP客户端都支持被动模式。

For example with WinSCP your batch file would be like:

例如,对于WinSCP,您的批处理文件如下:

@echo off

for %%A in (*.csv) do set latest=%%A 
echo Latest file is %latest%

winscp.com /command ^
    "open ftp://MYUSERNAME:MYPASSWORD@server58.hostinger.in/" ^
    "put -transfer=ascii %latest% s.txt" ^
    "exit"

Note that WinSCP defaults to the passive mode.

注意,WinSCP默认为被动模式。

For details see WinSCP guides for:

详情请参阅WinSCP指南:

(I'm the author of WinSCP)

(我是WinSCP的作者)