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?

···

--
Reese Chappuis

Try opening your ftp connection in passive mode;

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

Sam

···

On 19/09/11 10:45, Reese Chappuis wrote:

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?

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

···

On Mon, Sep 19, 2011 at 12:45 AM, Reese Chappuis <c0dege3k@gmail.com> 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?

--
Phillip Gawlowski

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/

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Sun, Sep 18, 2011 at 7:48 PM, Michael Bostler <mbostler@gmail.com> wrote:

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 Using FTP in Ruby | 4 Lines of Code

--
Posted via http://www.ruby-forum.com/\.

--
Reese Chappuis

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

···

On 19/09/11 13:18, Reese Chappuis 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.

On Sun, Sep 18, 2011 at 7:48 PM, Michael Bostler<mbostler@gmail.com> wrote:

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 Using FTP in Ruby | 4 Lines of Code

--
Posted via http://www.ruby-forum.com/\.

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 Wright

···

On Sep 18, 2011, at 9:18 PM, Reese Chappuis 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.

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 Sun, Sep 18, 2011 at 9:56 PM, Gary Wright <gwtmp01@mac.com> wrote:

On Sep 18, 2011, at 9:18 PM, Reese Chappuis 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 Wright

--
Reese Chappuis

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

···

On 20/09/11 08:49, Reese Chappuis wrote:

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 Sun, Sep 18, 2011 at 9:56 PM, Gary Wright<gwtmp01@mac.com> wrote:

On Sep 18, 2011, at 9:18 PM, Reese Chappuis 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 Wright