4

I'm trying to upload a file to a SFTP site using Net::SFTP.

The problem is the Ruby process never returns. It's frozen, stuck, never-ending... I have to Ctrl-C to make it stop.

  • I can connect to the ftp server in the command line using sftp.
  • I've tried this code on Linux (Ubuntu) and Snow Leopard and it does the same thing.
  • Installed the latest version of Net::SSH and Net::SFTP.
    • From gem list net:
      • net-sftp (2.0.5)
      • net-ssh (2.1.4)
  • Different combinations of hash arguments (see code below)
  • Tried to call Net::SFTP.start instead of creating a SSH session first.
  • Server responds with "SFTP protocol version 3" when I login using a command line tool and ask for the version.
  • I'm running Ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.7.0]

Debug output from :verbose (last couple of lines):

D, [2011-07-29T17:30:52.692192 #19399] DEBUG -- tcpsocket[80c06478]: read 36 bytes
D, [2011-07-29T17:30:52.692609 #19399] DEBUG -- tcpsocket[80c06478]: received packet nr 7 type 99 len 12
I, [2011-07-29T17:30:52.692751 #19399]  INFO -- net.ssh.connection.session[80bd5da0]: channel_success: 0
D, [2011-07-29T17:30:52.692817 #19399] DEBUG -- net.sftp.session[80bd5c24]: sftp subsystem successfully started
D, [2011-07-29T17:30:52.693625 #19399] DEBUG -- tcpsocket[80c06478]: queueing packet nr 8 type 94 len 28
D, [2011-07-29T17:30:52.693834 #19399] DEBUG -- tcpsocket[80c06478]: sent 52 bytes

Sample code:

require 'rubygems'
require 'net/sftp'

FTP = "someftpsite"
FTP_USER_NAME = "user"
FTP_PASSWORD = "password"
hash = {:password => FTP_PASSWORD, :verbose => :debug, :keys=>[""],:auth_methods => ["password"], :timeout => 5, :port=>22}
begin
    Net::SSH.start(FTP,FTP_USER_NAME, hash) do |test|
    test.sftp.upload!("localfile","remotefile")
end
rescue Exception => err
    puts "error:#{err.inspect}"
end

Edit: 8/22/2011

This seems to be related to the interactiveness of the SFTP server. I solved this by creating a shell script using expect and shelling out from Ruby to run the file. The shell script is created at run time. This seems really hackish, but it's the only way I've been able to make it work using Ruby. If anyone else has a suggestion, I would love to find a better way to do this.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user498023
  • 548
  • 1
  • 4
  • 9
  • 1
    possible the server authentication is set to interactive which requires handling login in your program differently... I am not expert in ruby so can't tell how this would be done in ruby – Yahia Jul 29 '11 at 22:49
  • Yahia, yes, I think you are on the right path. – user498023 Aug 01 '11 at 15:23

1 Answers1

1

You could use public/private key authentication instead. http://net-ssh.rubyforge.org/ssh/v1/chapter-2.html#s2

kristianp
  • 5,496
  • 37
  • 56