FTP ports

I’ve been writing a super simple terminal FTP client to send text files
from.

The code is:
require ‘net/ftp’

puts 'Enter the full path of the text file: ’
filename=gets.chomp
puts 'Enter the hostname of the remote computer: ’
hostname=gets.chomp
puts 'Enter the username for the remote computer: ’
uname = gets.chomp
puts 'Enter the password associated with the username: ’
pass = gets.chomp
puts 'Enter the path of the remote file: ’
rfilename = gets.chomp

def sendfile filename, rfilename=filename, hostname, uname, pass
ftp = Net::FTP.new(hostname)
puts “\nConnecting to “+hostname+”…”
ftp.login uname, pass
ftp.puttextfile filename, rfilename
ftp.close
end

sendfile filename, rfilename, hostname, uname, pass

Now, I’ve been getting this error: /usr/lib/ruby/1.9.1/net/ftp.rb:271:in
`getresp’: 425 Could not open data connection to port 22533: Connection
timed out (Net::FTPTempError)

But I don’t know why it is trying to go to that port- isn’t FTP port 21?
Is
there a way to change this?

Try opening your ftp connection in passive mode;

http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/index.html

Sam

On Mon, Sep 19, 2011 at 12:45 AM, Reese C. [email protected]
wrote:

But I don’t know why it is trying to go to that port- isn’t FTP port 21? Is
there a way to change this?

The FTP port is whatever port the server is listening on. Same for
webservers, mail servers, and so on. :wink:


Phillip G.

gplus.to/phgaw | twitter.com/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

maybe try using the connect method to manually specify the port number

rdoc at
http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/classes/Net/FTP.html#M001264

usage example at http://4loc.wordpress.com/2008/12/04/using-ftp-in-ruby/

The connect method suggestion didn’t work. And I just noticed another
thing-
the port number in the error keeps changing. In one test it was 11742,
and
in the next it was 11728.

Sorry, my suggestion may not have been that helpful as, despite the docs
I linked, you don’t seem to be able to get to the passive attribute
which looks like it is only set when you have SOCKS_PROXY env var set.
FTP uses more than one port, and you might have them firewalled. You
could try using a SOCKS proxy.

Sam

Thank you! That worked- and sorry Sam- the link you gave me just showed
me
to the net/ftp page, and I didn’t see anything about passive in there

On Sep 18, 2011, at 9:18 PM, Reese C. wrote:

The connect method suggestion didn’t work. And I just noticed another thing-
the port number in the error keeps changing. In one test it was 11742, and
in the next it was 11728.

ftp is an old protocol and has a somewhat unique architecture.

The command connection is initiated by the client towards port 21. When
an actual
file transfer is needed, the client begins listening on a local port
that is
picked by the OS (sometimes called an ephemeral port). The client then
tells the
server which port number was allocated via the control connection (11742
then
11728 in your instance).

At this point the server initiates a data connection to
the client towards the port number that was allocated by the client
system. This
is called ‘active mode’ in the protocol spec. This is where problems
usually
occur because many firewalls or administrative policies block that
incoming
TCP connection from the server to the client.

An alternative is to tell ftp to use ‘passive mode’. In this mode, the
server
picks an ephemeral port, tells the client what it picked via the command
connection, and then the client establishes the data connection towards
the
server. In this mode the two TCP connections are both outbound from the
client
and can often negotiate any firewalls as necessary.

You may be experiencing problems with either of these modes based on the
firewall policies between the client and the server.

You can set passive mode on as follows (based on quick look at net/ftp):

ftp = Net::FTP.new(hostname)
ftp.passive = true # make sure you do this, defaults to false
ftp.login uname, pass
ftp.puttextfile filename, rfilename
ftp.close

Gary W.

Hah, no problem. Glad you got there in the end. Evidently you /can/ get
to the passive attribute directly (although SOCKS_PROXY env var should
also work). I must’ve tried it on the wrong object in my haste.

Sam

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