-1

I am using the net-sftp gem to upload a file to an ftp server. Here's my code:

require "net/sftp"

Net::SFTP.start(url, username, password: password) do |sftp|
  sftp.upload!(file_path, "/")
end

It just hangs at the upload line, and eventually times out with the error Net::SSH::Disconnect: connection closed by remote host. I am able to connect via SFTP using FileZilla using the same url, username, and password.

I tried running the non-block version with verbose: :debugas well:

sftp = Net::SFTP.start(test.ftphost.com, ftp_username, password: ftp_password, verbose: :debug)

^ this produced output that shows that the connection was good:

I, [2015-04-29T10:32:51.381339 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_success: 0 D, [2015-04-29T10:32:51.381429 #25769] DEBUG -- net.sftp.session[3fc8a5025d70]: sftp subsystem successfully started

Then I entered the following:

sftp.upload!("/Users/marina/Desktop/test.png", "/")

The output is stuck like this:

I, [2015-04-29T10:32:55.035471 #25769]  INFO -- net.sftp.session[3fc8a5025d70]: sending open packet (0)
D, [2015-04-29T10:32:55.035740 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: queueing packet nr 12 type 94 len 44
D, [2015-04-29T10:32:55.036149 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: sent 68 bytes
D, [2015-04-29T10:32:55.119070 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: read 52 bytes
D, [2015-04-29T10:32:55.119356 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 11 type 96 len 28
I, [2015-04-29T10:32:55.119470 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_eof: 0
D, [2015-04-29T10:32:55.195747 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: read 120 bytes
D, [2015-04-29T10:32:55.196037 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 12 type 98 len 44
I, [2015-04-29T10:32:55.196176 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_request: 0 exit-status false
D, [2015-04-29T10:32:55.196445 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 13 type 97 len 28
I, [2015-04-29T10:32:55.196527 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_close: 0
D, [2015-04-29T10:32:55.196743 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: queueing packet nr 13 type 97 len 28
D, [2015-04-29T10:32:55.196806 #25769] DEBUG -- net.sftp.session[3fc8a5025d70]: sftp channel closed
D, [2015-04-29T10:32:55.197022 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: sent 52 bytes

Any ideas?

Marina
  • 3,222
  • 5
  • 25
  • 35

2 Answers2

2

What is file_path?

If you are using a relative path, it is going to be relative to the directory where the app starts.

The following may be useful:

Dir.pwd
Dir.entries('.')

Perhaps verbose: debug would be helpful.

Also:

I'm also having problems using Net::SFTP.start with a block and have had to make do with an inline approach.

sftp = Net::SFTP.start(host, username, ssh_session_options)
sftp.upload!(file.path, remote_path)
sftp.session.shutdown!

Github Issue

B Seven
  • 44,484
  • 66
  • 240
  • 385
  • my file_path looks like this: "/Users/marina/Desktop/test.png" – Marina Apr 29 '15 at 17:03
  • That looks good. You may want to check it with `ls /Users/marina/Desktop/test.png` – B Seven Apr 29 '15 at 17:27
  • yep, the filepath is valid. I updated my questions with the non-block version, including the verbose: :debug option. I've included the output. – Marina Apr 29 '15 at 17:45
  • @Marina I am having the same issue and I have noticed that it only hangs on png files. I see that it is also failing on a png for you. Have you had any luck in solving this? – Jordan Patterson Jul 23 '15 at 01:00
1

The issue was that I was putting the wrong target location. It shouldn't just be a directory, but the actual filename as well. So instead of saying the following

require "net/sftp"

Net::SFTP.start(url, username, password: password) do |sftp|
  sftp.upload!(file_path, "/")
end

I needed to add the filename:

require "net/sftp"

Net::SFTP.start(url, username, password: password) do |sftp|
  sftp.upload!(file_path, "/filename.extension")
end

And the block syntax did work for me.

Marina
  • 3,222
  • 5
  • 25
  • 35