Net:SSH question

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

Peter C. Verhage wrote:

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
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

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