Net:SSH question

P

Peter C. Verhage

Hi,

Is it possible to use Net:SSH to connect through multiple servers? E.g.
I need to use it for the following setup:

SSH to Server1 (using key)
--> SSH to Server2 (using login/password)
----> SSH to Server3 (using login/password)
------> SSH to Server4 (using login/password)
--------> Execute commands on Server4

Would that be possible today? Maybe by using port forwards on the next
servers SSH port to a local port and connect again to this port and
repeat the same trick?

Regards,

Peter
 
J

Jamis Buck

Peter said:
Hi,

Is it possible to use Net:SSH to connect through multiple servers? E.g.
I need to use it for the following setup:

SSH to Server1 (using key)
--> SSH to Server2 (using login/password)
----> SSH to Server3 (using login/password)
------> SSH to Server4 (using login/password)
--------> Execute commands on Server4

Would that be possible today? Maybe by using port forwards on the next
servers SSH port to a local port and connect again to this port and
repeat the same trick?

Haven't actually tried this, but the following (or something like it)
should do the trick. You'll probably need to use condition variables or
something to make sure the timing works out:

threads = []

threads.push Thread.new do
Net::SSH.start( 'host1', 'user' ) do |session|
mgr = PortForwardManager.new( session )
mgr.forward_local( 1234, 'host2', 22 )
session.main_loop
end
end

threads.push Thread.new do
Net::SSH.start( 'localhost', 1234, 'user', 'passwd' ) do |session|
mgr = PortForwardManager.new( session )
mgr.forward_local( 1235, 'host3', 22 )
session.main_loop
end
end

threads.push Thread.new do
Net::SSH.start( 'localhost', 1235, 'user', 'passwd' ) do |session|
mgr = PortForwardManager.new( session )
mgr.forward_local( 1236, 'host4', 22 )
session.main_loop
end
end

Net::SSH.start( 'localhost', 1236, 'user', 'passwd' ) do |session|
session.exec "rm -rf /"
end

threads.each { |t| t.join }

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 

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

Members online

Forum statistics

Threads
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top