Net::FTP hangs

Running ruby 1.8.5 (2006-08-25) on Mac OS X 10.4.8.

I’m trying to get this example from the Ruby_Cookbook to work, but it
hangs in the ftp.list method. What am I doing wrong?

#!/usr/bin/env ruby

require ‘net/ftp’

ftp = Net::FTP.open(“ftp.ibiblio.org”) do |ftp|
ftp.login
ftp.chdir(“pub/linux/”)
ftp.list(‘Linux’) {|file| puts file}
end

Eventually this will fail with these messages:

/opt/local/lib/ruby/1.8/net/ftp.rb:241:in getresp': 425 Unable to build data connection: Connection timed out (Net::FTPTempError) from /opt/local/lib/ruby/1.8/net/ftp.rb:264:insendcmd’
from /opt/local/lib/ruby/1.8/monitor.rb:238:in synchronize' from /opt/local/lib/ruby/1.8/net/ftp.rb:262:insendcmd’
from /opt/local/lib/ruby/1.8/net/ftp.rb:336:in transfercmd' from /opt/local/lib/ruby/1.8/net/ftp.rb:421:inretrlines’
from /opt/local/lib/ruby/1.8/monitor.rb:238:in synchronize' from /opt/local/lib/ruby/1.8/net/ftp.rb:419:inretrlines’
from /opt/local/lib/ruby/1.8/net/ftp.rb:625:in list' from ftptest.rb:8 from /opt/local/lib/ruby/1.8/net/ftp.rb:115:inopen’
from ftptest.rb:5

I can connect to this server and issue similar commands using the
command line ftp client:

ruby$ ftp
ftp> open ftp.ibiblio.org
Connected to ibiblio.org.
220 ProFTPD Server (Bring it on…)
Name (ftp.ibiblio.org:tim): anonymous
331 Anonymous login ok, send your complete email address as your
password.
Password:
230-
Welcome to ftp.ibiblio.org, the public ftp server of ibiblio.org. We
hope you find what you’re looking for.

If you have any problems or questions, please send email to

[email protected]

Thanks!

230 Anonymous access granted, restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub/linux
250 CWD command successful
ftp> ls Linux
229 Entering Extended Passive Mode (|||60724|)
150 Opening ASCII mode data connection for file list
-rw-r–r-- 1 (?) users 531113 Feb 26 11:06 00-find.Linux.gz
-rw-rw-r-- 1 (?) admin 73 Mar 9 2001
How-do-I-get-Linux
226 Transfer complete.
ftp>

Works for me from script or in irb. I’d be suspicious about your name
resolution

I’d suggest breaking this down - do any of the net examples work?
Try an http example
Try writing a dns client

Good luck

pboothOSXhome:~ pbooth$ uname -a
Darwin pboothOSXhome.local 8.8.2 Darwin Kernel Version 8.8.2: Thu Sep
28 20:43:26 PDT 2006; root:xnu-792.14.14.obj~1/RELEASE_I386 i386 i386
pboothOSXhome:~ pbooth$
pboothOSXhome:~ pbooth$ ruby ftpscript.rb
-rw-r–r-- 1 (?) users 531113 Feb 26 11:06 00-find.Linux.gz
-rw-rw-r-- 1 (?) admin 73 Mar 9 2001 How-do-I-get-
Linux
pboothOSXhome:~ pbooth

Last login: Mon Feb 26 21:18:06 on console
Welcome to Darwin!
pboothOSXhome:~ pbooth$ irb
irb(main):001:0> require ‘net/ftp’
=> true
irb(main):002:0> ftp = Net::FTP.open(“ftp.ibiblio.org”) do |ftp|
irb(main):003:1* ftp.login
irb(main):004:1> ftp.chdir(“pub/linux”)
irb(main):005:1> ftp.list(‘Linux’) {|f| puts f}
irb(main):006:1> end
-rw-r–r-- 1 (?) users 531113 Feb 26 11:06 00-find.Linux.gz
-rw-rw-r-- 1 (?) admin 73 Mar 9 2001 How-do-I-get-
Linux
=> nil
irb(main):007:0> quit
pboothOSXhome:~ pbooth$ ruby -v
ruby 1.8.5 (2006-08-25) [powerpc-darwin8.8.0]
pboothOSXhome:~ pbooth$

Peter B. wrote:

Works for me from script or in irb. I’d be suspicious about your name
resolution

I’d suggest breaking this down - do any of the net examples work?
Try an http example
Try writing a dns client

open-uri works to retrieve a web page via a url. curl works. I installed
the most recent version of Ruby but it didn’t help.

On Wed, 28 Feb 2007, Timothy H. wrote:

ftp.passive = true

??

-a

[email protected] wrote:

open-uri works to retrieve a web page via a url. curl works. I
installed the most recent version of Ruby but it didn’t help.

ftp.passive = true

??

-a
That did the trick, Ara! Thanks! Now the question is, why?

On 2/28/07, Timothy H. [email protected] wrote:

[email protected] wrote:

ftp.passive = true

??

-a
That did the trick, Ara! Thanks! Now the question is, why?

This has to do with the proliferation of FTP servers hidden behind
firewalls over the last few years. There’s a fairly detailed
explanation here:

http://slacksite.com/other/ftp.html

If you’d like more information, just google passive FTP.

[email protected] wrote:

-a
Thank you for the education, Ara. My ignorance of FTP is a bit less vast
than before, thanks to you.

On Thu, 1 Mar 2007, Timothy H. wrote:

most client software auto negotiates this these days, like curl, which
is very
smart. ruby’s ftp lib doesn’t. i always forget which is which between
passive and active, but i think people are moving toward passive because
it
plays nice with firewalls.

http://slacksite.com/other/ftp.html

regards.

-a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs