Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby net-ssh wirth proxy command causes freeze

I would like to connect to a remote computer via another using ruby.

This scheme is the following :

Local -> proxy -> remote

I have this code which is doing the work for a direct access :

require 'net/ssh'
Net::SSH.start(remote_host, remote_user) do |ssh|
    puts ssh.exec!'hostname'
end

However, when I try with the proxy, the command 'hostname' is executed and correct, but then the code freezes, same if I call ssh.close.

Here is the code :

require 'net/ssh'
require 'net/ssh/proxy/command'

proxy_cmd = Net::SSH::Proxy::Command.new('ssh proxy_user@proxy_host nc %h %p')
Net::SSH.start(remote_host, remote_user, :proxy => proxy) do |ssh|
    puts ssh.exec!'hostname'
end

The loggin is done without password thanks to a rsa key. And the proxycommand is working (I was using it in bash before)

Would someone knows what I am doing wrong ?

Thank you very much for your interest,

EDIT : here is the last line in the logs, it blocks there :

I, [2013-10-16T23:01:19.304778 #3785]  INFO -- net.ssh.connection.session[4555128]: closing remaining channels (0 open)
like image 425
j.flajo Avatar asked Oct 02 '22 16:10

j.flajo


1 Answers

I've just bumped in the same issue - command line ssh was working and net/ssh was hanging on me when using proxycommand.

Debuging net/ssh brought me as far as: https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/transport/session.rb#L113 and the whole thing was hanging on the .close call of the socket.

I'm not sure what caused this, but adding timeout to nc command seems to have solved it:

ProxyCommand ssh proxy_server@proxy_server nc -q 1 %h %p

like image 62
madsheep Avatar answered Oct 17 '22 21:10

madsheep