Search code examples
rubyftpftp-client

Ruby Passive FTP problems (Windows NT Server - Net::FTPReplyError, Net::FTPProtoError)


I'm having trouble using net/ftp to connect to a Windows NT ftp server.

It seems some other clients are having trouble connecting to this server, but FileZilla has no problem at all.

Here is the debug output from ruby's connection attempt:

[151] pry(main):1> ftp = Net::FTP.new(ftp_address)
=> #<Net::FTP:0x0000010e236fe0
 @binary=true,
 @debug_mode=false,
 @last_response="220 Microsoft FTP Service\n",
 @last_response_code="220",
 @logged_in=false,
 @mon_count=0,
 @mon_mutex=#<Mutex:0x0000010e236f90>,
 @mon_owner=nil,
 @open_timeout=nil,
 @passive=false,
 @read_timeout=60,
 @resume=false,
 @sock=#<Net::FTP::BufferedSocket io=#<TCPSocket:0x0000010e236f18>>>
[152] pry(#<Site>):1> ftp.debug_mode = true
=> true
[153] pry(#<Site>):1> ftp.passive = true      
=> true
[154] pry(#<Site>):1> ftp.login(ftp_user, ftp_password)      
put: USER <redacted>
get: 331 Password required for <redacted>.
put: PASS ********
get: 230 User logged in.
put: TYPE I
get: 200 Type set to I.
=> true
[155] pry(#<Site>):1> ftp.chdir("/")
put: CWD /
get: 250 CWD command successful.
=> nil
[156] pry(main):1> ftp.putbinaryfile("/Users/home/Desktop/testupload.csv")
put: PASV
get: 27 Entering Passive Mode (0,1,2,3,192,119)
Net::FTPReplyError: 27 Entering Passive Mode (0,1,2,3,192,119)
from /Users/home/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/net/ftp.rb:978:in `parse227'
[157] pry(main):1> ftp.putbinaryfile("/Users/home/Desktop/testupload.csv")
put: PASV
get: 
Net::FTPProtoError: 
from /Users/home/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/net/ftp.rb:329:in `getresp'

And here is the output from filezilla:

Status: Resolving address of <redacted host>
Status: Connecting to <redacted ip>:21...
Status: Connection established, waiting for welcome message...
Response: 220 Microsoft FTP Service
Command:  USER <redacted>
Response: 331 Password required for <redacted>.
Command:  PASS ********
Response: 230 User logged in.
Command:  OPTS UTF8 ON
Response: 200 OPTS UTF8 command successful - UTF8 encoding now ON.
Status: Connected
Status: Retrieving directory listing...
Command:  PWD
Response: 257 "/" is current directory.
Status: Directory listing successful
Status: Resolving address of <redacted host>
Status: Connecting to <redacted ip>:21...
Status: Connection established, waiting for welcome message...
Response: 220 Microsoft FTP Service
Command:  USER <redacted>
Response: 331 Password required for <redacted>.
Command:  PASS ********
Response: 230 User logged in.
Command:  OPTS UTF8 ON
Response: 200 OPTS UTF8 command successful - UTF8 encoding now ON.
Status: Connected
Status: Starting upload of /Users/home/Desktop/testupload.csv
Command:  CWD /
Response: 250 CWD command successful.
Command:  TYPE I
Response: 200 Type set to I.
Command:  PASV
Response: 27 Entering Passive Mode (0,1,2,3,192,121)
Command:  STOR testupload.csv
Response: 150 Opening BINARY mode data connection.
Response: 226 Transfer complete.
Status: File transfer successful, transferred 97 bytes in 1 second
Status: Retrieving directory listing...
Command:  PASV
Response: 27 Entering Passive Mode (0,1,2,3,192,122)
Command:  LIST
Response: 150 Opening BINARY mode data connection.
Response: 226 Transfer complete.
Status: Directory listing successful

(note that in the above 27 Entering Passive Mode (0,1,2,3,192,122) lines, 0,1,2,3 was the IP address of the server we're connecting to).

Any idea what might cause this? I've been getting similar (to the ruby response) strange FTP responses when using CyberDuck as a client.


Solution

  • The FTP server sends an invalid response 27 Entering passive mode.... The response must start with a response code of 3 digits, e.g. the correct response in this case would be 227 Entering passive mode.... FileZilla is just lazy in checking the correct response and thus accepts the wrong one. But the problem itself is caused by the server.