16

如果我在本地执行此操作,一切正常:

require 'net/ftp'

ftp=Net::FTP.new("myftpserver.com", "username", "password")

ftp.getbinaryfile("/myfile.zip","localfile.zip")
ftp.close

如果我尝试在我正在使用的 Linux 服务器上执行它,结果是:

/usr/local/lib/ruby/1.9.1/net/ftp.rb:273:in `getresp': 500 Illegal
PORT command. (Net::FTPPermError)   from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:281:in `voidresp'  from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:304:in `block in voidcmd'  from
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'   from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:302:in `voidcmd'   from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:317:in `sendport'  from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:325:in `makeport'  from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:358:in `transfercmd'   from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:420:in `block (2 levels) in
retrbinary'   from /usr/local/lib/ruby/1.9.1/net/ftp.rb:166:in
`with_binary'   from /usr/local/lib/ruby/1.9.1/net/ftp.rb:419:in `block
in retrbinary'  from /usr/local/lib/ruby/1.9.1/monitor.rb:201:in
`mon_synchronize'   from /usr/local/lib/ruby/1.9.1/net/ftp.rb:418:in
`retrbinary'  from /usr/local/lib/ruby/1.9.1/net/ftp.rb:539:in
`getbinaryfile'

可能是什么问题呢?

4

1 回答 1

39

我在http://www.ruby-forum.com/topic/161274找到了答案:

在防火墙之外,活动 ftp 无法在 NAT 设备后面工作。如果您告诉 Ftp 服务器您的地址是私有 IP 地址,例如 192.168.x(您在 nat 设备后面的网络上的地址),则 Ftp 服务器有时会说出非法端口命令

添加:

ftp.passive = true

解决它。

于 2012-12-06T21:37:39.597 回答