gal
gal

Reputation: 779

ruby - check ssh connection to a remote through another machine

I am using aws sdk in order to create machines, and as soon as the machines are created, i want to check ssh connection to those machines. the problem is - from my machines, i don't have access to EC2 machines, but there is another machine on my network, which i have access to through ssh, and this machine do have access to the EC2 machines. Now, is there a simple way through ruby to check ssh to EC2 machine ?

I've tried the following:

proxy = Net::SSH::Proxy::HTTP.new('<proxy ip>', 22)
Net::SSH.start('<EC2 machine's IP>', '<USER>', :proxy => proxy) do |ssh|
end
Errno::ECONNRESET: Connection reset by peer
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:76:in `gets'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:76:in `parse_response'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/proxy/http.rb:62:in `open'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `block in initialize'
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:67:in `initialize'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `new'
from /home/galt/.rvm/gems/ruby-1.9.3-p484/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `start'
from (irb):12
from /home/galt/.rvm/rubies/ruby-1.9.3-p484/bin/irb:12:in `<main>'

thanks !

Upvotes: 1

Views: 537

Answers (1)

Douglas Camata
Douglas Camata

Reputation: 597

The Net::SSH::Proxy::HTTP class is meant to point to a HTTP proxy for your ssh connections. So, you will need to set up a HTTP proxy on that machine to be able to proxy your connections through it.

If you can't do that, I suggest you use SSH reverse tunnels. We will achieve this by opening the port 8080 on the local machine, forward the traffic sent there to Server using the SSH tunnel and make Server forward it to the EC2 instance on port 22. The net-ssh ruby gem already provide commands to help us with that, but we will need to run 2 sessions in parallel, using threads:

proxy_thread = Thread.new do
  Net::SSH.start('<proxy ip>', '<user>') do |ssh|
    ssh.forward.remote(8080, "<AWS ip>", 22)
    ssh.loop { true }
  end
end

proxy_thread.start

Net::SSH.start('localhost', '<localhost user>', port: 8080) do |ssh|
  #done
end

proxy_thread.kill

Upvotes: 1

Related Questions