net-ssh hanging?

  • Thread starter Carl Youngblood
  • Start date
C

Carl Youngblood

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
 
J

Jamis Buck

Carl said:
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.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

.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top