# Using net:ssh

**URL:** <https://www.ruby-forum.com/t/using-net-ssh/129550>\
**Category:** Ruby\
**Created:** [February 19, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/using-net-ssh/129550 "2008-02-19T14:15:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [February 19, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/using-net-ssh/129550/1 "2008-02-19T14:15:34Z")

</div>

Hello,  
what I want to do is the following:

- ssh into a remote machine (let’s call it ‘frontend’)
- from the front-end, ssh into another machine (call it node-A)

So far, i was only able to perform the first step:

require ‘rubygems’  
require ‘net/ssh’

def do\_run\_introducer(session)  
session.open\_channel do |channel|  
channel.on\_data do |ch, data|  
puts data  
end  
channel.exec “ls” #to verify that i’m actually logged in…  
end  
end

Net::SSH.start( ‘remote-machine-address’, my-user-name’ ) do |  
session|  
do\_run\_introducer session  
#do\_run\_peers session  
session.loop  
end

Any help is very appreciated

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [February 19, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/using-net-ssh/129550/2 "2008-02-19T14:31:03Z")

</div>

On Feb 19, 5:14 am, Valerio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> what I want to do is the following:
> 
> - ssh into a remote machine (let’s call it ‘frontend’)
> - from the front-end, ssh into another machine (call it node-A)

I’ve not used the Net::SSH library personally, so I can’t give any  
specific help; but you might take a look through the code for  
Capistrano ([http://rubyforge.org/projects/capistrano](http://rubyforge.org/projects/capistrano)). IIRC, it  
supports tunneling an SSH connection through a “frontend” machine, so  
you may be able to figure it out by browsing the source.

–  
Regards,

John W.

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [February 19, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/using-net-ssh/129550/3 "2008-02-19T14:46:23Z")

</div>

On Tue, 2008-02-19 at 22:15 +0900, Valerio S. wrote:

> def do\_run\_introducer(session)  
> do\_run\_introducer session  
> #do\_run\_peers session  
> session.loop  
> end
> 
> Any help is very appreciated

Use port forwarding to tunnel a connection through the front end to the  
SSH port on the node behind it. Then call a second SSH session on the  
tunneled port.

See [http://net-ssh.rubyforge.org/chapter-6.html](http://net-ssh.rubyforge.org/chapter-6.html) for port forwarding.

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![Valerio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@Valerio\_S](https://www.ruby-forum.com/u/Valerio_S)\
**Post date:** [February 19, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/using-net-ssh/129550/4 "2008-02-19T15:05:54Z")

</div>

On Feb 19, 2008 7:15 AM, Valerio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> what I want to do is the following:
> 
> - ssh into a remote machine (let’s call it ‘frontend’)
> - from the front-end, ssh into another machine (call it node-A)

I’m not sure if Net::SSH supports this, but basically what you want is  
this:

```
$ ssh -f -L 1234:remote_host:22 proxy_host < /dev/null &> /dev/null 

```

&

```
$ ssh -p 1234 localhost

```

where “1234” is a number you pick. It will be the port on the client  
machine that you can use to initiate a SSH connection to the remote  
machine (remote\_host). Note that “remote\_host” will be resolved by  
“proxy\_host”, not by the client.

I’d suggest you try it first on the command line and once you get it  
working wrap it with Ruby.

HTH,

Marcelo
