Why can't Net::FTP connect to server?
Asked Answered
V

2

13

I am trying to create a script to list and download data from a FTP server with Ruby. I am new to Ruby so I looked for documentation how to use Net::FTP. I have trouble understanding why this doesn't work:

require 'net/ftp'

server = "ftp.server.com"
user = "myuser"
password = "mypassword"


Net::FTP.open(server, user, password) do |ftp|
        files = ftp.chdir('mydirectory/')
        files = ftp.list
        puts "list out of directory:"
        puts files
end

That doesn't work, returning this error:

/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:298:in `getresp': 425 >Failed to establish connection. (Net::FTPTempError)
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:325:in `block in sendcmd'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:323:in `sendcmd'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:402:in `transfercmd'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:478:in `block (2 levels) in retrlines'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:178:in `with_binary'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:477:in `block in retrlines'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:476:in `retrlines'
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:722:in `list'
    from test_ftp.rb:10:in `block in '
    from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:116:in `open'
    from test_ftp.rb:8:in `'

Can anyone explain what's wrong with my script?

Vary answered 27/2, 2013 at 3:47 Comment(0)
P
40

Your code works fine for me. I suspect problem could be because of Net::FTP connection mode, which is by default active. Try connecting using passive mode, following code sample -

ftp = Net::FTP.new(server)
ftp.passive = true
ftp.login user, password
files = ftp.chdir('mydirectory/')
files = ftp.list
puts "list out of directory:"
puts files
ftp.close

And if you're curious, following is the difference (from wikipedia) between active and passive modes.

  1. In Active mode, the client creates a TCP control connection to the server and sends the server the client's IP address and an arbitrary client port number, and then waits until the server initiates the data connection over TCP to that client IP address and client port number. In situations where the client is behind a firewall and unable to accept incoming TCP connections, passive mode may be used.
  2. In Passive mode, the client uses the control connection to send a PASV command to the server and then receives a server IP address and server port number from the server which the client then uses to open a data connection from an arbitrary client port to the server IP address and server port number received.
Phone answered 27/2, 2013 at 4:50 Comment(4)
Thanks Ashish, I change Net:FTP mode to passive, and it's work without I change ftp server configuration.. :)Vary
@Vary if it helped then you should accept the answer. Doing so not only adds little to your reputation but goes a long way in ensuring that people keep responding to your queries.Phone
@Ashish.. sorry Ashish I am new to stackoverflow, I don't understand what do you mean.. what should I do?Vary
why is active defaultPossum
F
2

The following script works from my machine, and is based on yours, with a minor cleanup:

require 'net/ftp'

Net::FTP.open('ftp.linuxjournal.com', 'anonymous', '[email protected]') do |ftp|
  ftp.chdir('pub/')
  files = ftp.list
  puts "list out of directory:"
  puts files
end

This is the output I get:

list out of directory:
lrwxrwxrwx   1 root     ftp            32 Jul 12  2010 00-README-TO-UPLOAD-FILES.txt -> ../00-README-TO-UPLOAD-FILES.txt
drwxr-sr-x   3 root     ftp            21 Mar  7  2001 elj
drwxr-sr-x   3 root     ftp          4096 Jan  6  2004 lg
drwxr-sr-x   8 root     ftp           113 Jun 21  2010 lj
drwxr-sr-x   5 root     users         112 Oct  2  2007 ssc
drwxr-sr-x   2 root     ftp          4096 Jan  3 17:21 tux

I'd say the code works, and the problem is elsewhere.

Because of 425 >Failed to establish connection I think you should check your DNS and/or firewalls.

Fireplug answered 27/2, 2013 at 4:43 Comment(1)
yaaa, I already test my code via irb, that 's code can login but when I try to list, the error appera. I think the problem relate with my ftp server configuration.. thanks for advice. :)Vary

© 2022 - 2024 — McMap. All rights reserved.