FTPS - How to connect with Ruby?

Discussion in 'Ruby' started by Jan Veselý, Mar 20, 2007.

  1. Jan Veselý

    Jan Veselý Guest

    Hello, I'm new to Ruby and have the first problem I can't solve myself
    after a lot of googling - connecting to a FTPS server (FTP AUTH TLS/
    SSL).

    - there's some library called chilkat, but it's not free (for this
    little script it's really too expensive to solve it that way)
    - ftpfxp library is targeted on fxp transfers, but it connects ok -
    but it doesn't seem like a good library for normal ftp-related work
    - the standard ftp library doesn't look like it has some support for
    firewall/proxy to get there a program like TLSWrap between client and
    server and solve the problem
    - I found some curl library in prealfa state that could possibly help
    me somehow - but it looks like the last solution when everything else
    fails (prealfa, weird installation)

    Could you please give me a hint?
    Jan Veselý, Mar 20, 2007
    #1
    1. Advertising

  2. Jan Veselý

    ChrisKaelin Guest

    On 20 Mrz., 17:42, "Jan Veselý" <> wrote:
    > Hello, I'm new to Ruby and have the first problem I can't solve myself
    > after a lot of googling - connecting to a FTPS server (FTP AUTH TLS/
    > SSL).
    >
    > - there's some library called chilkat, but it's not free (for this
    > little script it's really too expensive to solve it that way)
    > - ftpfxp library is targeted on fxp transfers, but it connects ok -
    > but it doesn't seem like a good library for normal ftp-related work
    > - the standard ftp library doesn't look like it has some support for
    > firewall/proxy to get there a program like TLSWrap between client and
    > server and solve the problem
    > - I found some curl library in prealfa state that could possibly help
    > me somehow - but it looks like the last solution when everything else
    > fails (prealfa, weird installation)
    >
    > Could you please give me a hint?



    Try net/sftp Module. With that I made a backup script on window$ using
    scp and/or sftp, because rsync is not available on the server, I have
    to back up.

    net/sftp project: http://raa.ruby-lang.org/project/net-sftp/

    Here's an example, how it's working:

    arr = Array.new
    Net::SFTP.start(@host, @user, @pass) do |sftp|
    handle = sftp.opendir(dir)
    items = sftp.readdir(handle)
    items.each do |item|
    arr.push(item)
    end
    sftp.close_handle(handle)
    end
    return arr

    For more infos you should check the project's page.

    Greetings

    Chris
    ChrisKaelin, Mar 20, 2007
    #2
    1. Advertising

  3. Jan Veselý

    Guest

    On Mar 20, 10:40 am, "ChrisKaelin" <> wrote:
    > On 20 Mrz., 17:42, "Jan Veselý" <> wrote:
    >
    >
    >
    > > Hello, I'm new to Ruby and have the first problem I can't solve myself
    > > after a lot of googling - connecting to a FTPS server (FTP AUTH TLS/
    > > SSL).

    >
    > > - there's some library called chilkat, but it's not free (for this
    > > little script it's really too expensive to solve it that way)
    > > - ftpfxp library is targeted on fxp transfers, but it connects ok -
    > > but it doesn't seem like a good library for normal ftp-related work
    > > - the standard ftp library doesn't look like it has some support for
    > > firewall/proxy to get there a program like TLSWrap between client and
    > > server and solve the problem
    > > - I found some curl library in prealfa state that could possibly help
    > > me somehow - but it looks like the last solution when everything else
    > > fails (prealfa, weird installation)

    >
    > > Could you please give me a hint?

    >
    > Try net/sftp Module. With that I made a backup script on window$ using
    > scp and/or sftp, because rsync is not available on the server, I have
    > to back up.
    >
    > net/sftp project:http://raa.ruby-lang.org/project/net-sftp/
    >


    Unfortunately, sftp is a different protocol than FTPS and that module
    probably won't help.

    (3 months ago I received a spec saying my company would communicate
    with another company using SFTP, only to figure out after code had
    been written that they meant FTPS.)

    I looked in my install of ruby and found a file net/ftptls.rb. That
    code looks like it's only for Implicit FTPS, not explicit. I'd try it
    anyway.

    The code I ended up writing (in Java) was a class that interfaced to
    the curl command line client. It worked very well. As dirty as it
    seemed at the time, I still think it was better than writing JNI to
    the curl C interface.

    Here is a quick and dirty class to interface to the curl command line
    client from ruby. This should work for FTPS, if your curl client was
    compiled with it. curl -V will list the protocols it supports.

    class CurlFtp

    CURL = "curl"

    def initialize(url, user, pass, verbose=false)
    @url = url
    @user = user
    @pass = pass
    @verboseFlag = (verbose) ? '-v' : ''
    end

    def put(path, &block)
    command = sprintf "'%s' %s --upload-file - --user '%s:%s' '%s/
    %s'",
    CURL, @verboseFlag, @user, @pass, @url, path
    IO.popen(command, "w", &block)
    raise "Curl error: #{$?.to_i / 256}" if $? != 0
    end

    def get(path, &block)
    command = sprintf "'%s' %s --user '%s:%s' '%s/%s'",
    CURL, @verboseFlag, @user, @pass, @url, CGI.escape(path)
    IO.popen(command, "r", &block)
    raise "Curl error: #{$?.to_i / 256}" if $? != 0
    end

    end

    ftp = CurlFtp.new('ftp://localhost:21', 'bcastill', 'pass', false)

    ftp.put('/upload/file.txt') do |io|
    io.puts("Writing to FTP server....")
    end

    puts "Getting data:"
    ftp.get('/upload/file.txt') do |io|
    io.each_line do |line|
    puts "LINE: #{line}"
    end
    end
    , Mar 21, 2007
    #3
  4. Jan Veselý

    Guest

    On Mar 20, 10:51 pm, wrote:
    > On Mar 20, 10:40 am, "ChrisKaelin" <> wrote:
    >
    >
    >
    > > On 20 Mrz., 17:42, "Jan Veselý" <> wrote:

    >
    > > > Hello, I'm new to Ruby and have the first problem I can't solve myself
    > > > after a lot of googling - connecting to a FTPS server (FTP AUTH TLS/
    > > > SSL).

    >
    > > > - there's some library called chilkat, but it's not free (for this
    > > > little script it's really too expensive to solve it that way)
    > > > - ftpfxp library is targeted on fxp transfers, but it connects ok -
    > > > but it doesn't seem like a good library for normal ftp-related work
    > > > - the standard ftp library doesn't look like it has some support for
    > > > firewall/proxy to get there a program like TLSWrap between client and
    > > > server and solve the problem
    > > > - I found some curl library in prealfa state that could possibly help
    > > > me somehow - but it looks like the last solution when everything else
    > > > fails (prealfa, weird installation)

    >
    > > > Could you please give me a hint?

    >
    > > Try net/sftp Module. With that I made a backup script on window$ using
    > > scp and/or sftp, because rsync is not available on the server, I have
    > > to back up.

    >
    > > net/sftp project:http://raa.ruby-lang.org/project/net-sftp/

    >
    > Unfortunately, sftp is a different protocol than FTPS and that module
    > probably won't help.
    >
    > (3 months ago I received a spec saying my company would communicate
    > with another company using SFTP, only to figure out after code had
    > been written that they meant FTPS.)
    >
    > I looked in my install of ruby and found a file net/ftptls.rb. That
    > code looks like it's only for Implicit FTPS, not explicit. I'd try it
    > anyway.
    >
    > The code I ended up writing (in Java) was a class that interfaced to
    > the curl command line client. It worked very well. As dirty as it
    > seemed at the time, I still think it was better than writing JNI to
    > the curl C interface.
    >
    > Here is a quick and dirty class to interface to the curl command line
    > client from ruby. This should work for FTPS, if your curl client was
    > compiled with it. curl -V will list the protocols it supports.
    >
    > class CurlFtp
    >
    > CURL = "curl"
    >
    > def initialize(url, user, pass, verbose=false)
    > @url = url
    > @user = user
    > @pass = pass
    > @verboseFlag = (verbose) ? '-v' : ''
    > end
    >
    > def put(path, &block)
    > command = sprintf "'%s' %s --upload-file - --user '%s:%s' '%s/
    > %s'",
    > CURL, @verboseFlag, @user, @pass, @url, path
    > IO.popen(command, "w", &block)
    > raise "Curl error: #{$?.to_i / 256}" if $? != 0
    > end
    >
    > def get(path, &block)
    > command = sprintf "'%s' %s --user '%s:%s' '%s/%s'",
    > CURL, @verboseFlag, @user, @pass, @url, CGI.escape(path)
    > IO.popen(command, "r", &block)
    > raise "Curl error: #{$?.to_i / 256}" if $? != 0
    > end
    >
    > end
    >


    (In case it wasn't obvious, you should change ftp to ftps in the url
    and you may need to supply a port.)

    > ftp = CurlFtp.new('ftp://localhost:21', 'bcastill', 'pass', false)
    >
    > ftp.put('/upload/file.txt') do |io|
    > io.puts("Writing to FTP server....")
    > end
    >
    > puts "Getting data:"
    > ftp.get('/upload/file.txt') do |io|
    > io.each_line do |line|
    > puts "LINE: #{line}"
    > end
    > end
    , Mar 21, 2007
    #4
  5. Jan Veselý

    Jan Veselý Guest

    Thank you for your reply! I should mention the difference between sftp
    and ftps to avoid confusion.

    Good point with this ftptls library but it can't encrypt the data
    channel, just the command channel.

    This curl solution is probably ok but as I post in the first post, I
    consider this as the last possibility.

    Nevertheless, I found the ultimate solution ;) This FTPFXPTLS can be
    used with no problems as a normal ftp, it was just my lack of
    understanding Ruby. So if anyone is interested: use gem to install
    ftpfxp and then use FTPFXPTLS instead of ftp. It's working like a
    charm but nowhere on the net mentioned...

    Jan
    Jan Veselý, Mar 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Avtanski

    Free FTPS client implementation?

    Alexander Avtanski, Mar 1, 2006, in forum: Java
    Replies:
    4
    Views:
    10,089
  2. Robert
    Replies:
    2
    Views:
    1,962
    Robert
    Jun 10, 2004
  3. Russell E. Owen

    trouble with urllib and ftps

    Russell E. Owen, Nov 17, 2004, in forum: Python
    Replies:
    0
    Views:
    344
    Russell E. Owen
    Nov 17, 2004
  4. Peter A. Schott

    FTPLIB & FTPS or SFTP?

    Peter A. Schott, Jan 19, 2005, in forum: Python
    Replies:
    3
    Views:
    536
    Peter A. Schott
    Jan 20, 2005
  5. Innokentiy Ivanov

    Running FTPS from the ASP.NET page

    Innokentiy Ivanov, May 29, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    412
    sqlman
    Jun 3, 2009
Loading...

Share This Page