Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Could not open data connection to port xxxx" when uploading file to FTP from Windows batch file

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

Port command sent successfully

but after that shows

Could not open data connection to port xxxx
Connection timed out

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
like image 362
Safdar Iqbal Avatar asked Jun 11 '15 05:06

Safdar Iqbal


1 Answers

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.

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.

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


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.

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

For example with WinSCP your batch file would be like:

@echo off

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

winscp.com /command ^
    "open ftp://MYUSERNAME:[email protected]/" ^
    "put -transfer=ascii %latest% s.txt" ^
    "exit"

Note that WinSCP defaults to the passive mode.

For details see WinSCP guides for:

  • Automating file transfers to FTP server
  • Converting Windows FTP script to WinSCP script

(I'm the author of WinSCP)

like image 98
Martin Prikryl Avatar answered Sep 28 '22 08:09

Martin Prikryl