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)
- From
- 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.