Net-ssh hanging?

Okay, I set up ruby again and net-ssh appears to be working properly.
At least, rpa didn't give me any errors during install and I have been
able to get some of it to work. However, when I run the following
little test app, it successfully copies the two files to the server,
but it hangs indefinitely after it's finished. I thought that when I
started a session in block mode it was supposed to exit automatically
after everything was finished. Is there something I'm missing?

Thanks,
Carl

···

--------------------------

#!/usr/bin/env ruby

require 'net/ssh'
require 'net/ssh/sftp'

Net::SSH.start('host', 'user', 'pass') do |session|
  Net::SSH::SFTP.session(session) do |sftp|
    sftp.put('file1', 'contents') do |status|
      if status[:code] != Net::SSH::SFTP::Session::SSH_FX_OK
        raise "error (#{status[:code]}, #{status[:message]})"
      else
        puts "success"
      end
    end
    sftp.put('file2', 'contents') do |status|
      if status[:code] != Net::SSH::SFTP::Session::SSH_FX_OK
        raise "error (#{status[:code]}, #{status[:message]})"
      else
        puts "success"
      end
    end
  end
  session.main_loop
end

Carl Youngblood wrote:

Okay, I set up ruby again and net-ssh appears to be working properly. At least, rpa didn't give me any errors during install and I have been
able to get some of it to work. However, when I run the following
little test app, it successfully copies the two files to the server,
but it hangs indefinitely after it's finished. I thought that when I
started a session in block mode it was supposed to exit automatically
after everything was finished. Is there something I'm missing?

Thanks,
Carl

Carl, my apologies for not responding to this sooner.

You need to call 'sftp.shutdown' to close the channels that the SFTP session opens. Otherwise, session.mainloop will loop forever, waiting for those channels to close.

However, you can't just do:

   sftp.put(...) { ... }
   sftp.shutdown

If you do, your program will terminate without transferring anything (due to the asynchronous nature of SFTP). Instead, you have to have each subsequent reuqest occur after the previous request finished:

   sftp.put(...) do
     sftp.put(...) do
       sftp.shutdown
     end
   end
   sftp.main_loop

Does that make sense? If that is still not very clear, let me know and I'll hack on your source code and send you one that works as described above.

Hope that helps, and good luck, Carl!

- Jamis

···

--------------------------

#!/usr/bin/env ruby

require 'net/ssh'
require 'net/ssh/sftp'

Net::SSH.start('host', 'user', 'pass') do |session|
  Net::SSH::SFTP.session(session) do |sftp|
    sftp.put('file1', 'contents') do |status|
      if status[:code] != Net::SSH::SFTP::Session::SSH_FX_OK
        raise "error (#{status[:code]}, #{status[:message]})" else
        puts "success"
      end
    end
    sftp.put('file2', 'contents') do |status|
      if status[:code] != Net::SSH::SFTP::Session::SSH_FX_OK
        raise "error (#{status[:code]}, #{status[:message]})" else
        puts "success"
      end
    end
  end
  session.main_loop
end

.

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

Makes perfect sense. Thanks Jamis!

···

On Sun, 10 Oct 2004 23:24:43 +0900, Jamis Buck <jgb3@email.byu.edu> wrote:

Does that make sense? If that is still not very clear, let me know and
I'll hack on your source code and send you one that works as described
above.