# Usin net/ssh library

**URL:** <https://www.ruby-forum.com/t/usin-net-ssh-library/139185>\
**Category:** Ruby\
**Created:** [June 1, 2008, 11:23am UTC](https://www.ruby-forum.com/t/usin-net-ssh-library/139185 "2008-06-01T11:23:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [June 1, 2008, 11:23am UTC](https://www.ruby-forum.com/t/usin-net-ssh-library/139185/1 "2008-06-01T11:23:54Z")

</div>

Hi,  
I was using the net/ssh library in my code ro ssh to a particular  
machine.  
But I found that the ssh library goes directly to the shell prompt and  
executes the commands. net/ssh uses the ssh2 protocol internally, I also  
tried using ssh1  
but with no success.

Initially the prompt that appears should have been something like “\>\>\>\>”  
but  
in my case after running the script it goes directly to the shell prompt  
“$” by-passing the cli prompt.

a. Why does this behaviour take place??

b. Is there a way to go to the initial cli prompt ??

-P

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [June 2, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/usin-net-ssh-library/139185/2 "2008-06-02T13:12:45Z")

</div>

On Sun, 1 Jun 2008 18:23:25 +0900  
Prasad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but  
> in my case after running the script it goes directly to the shell prompt  
> “$” by-passing the cli prompt.
> 
> a. Why does this behaviour take place??
> 
> b. Is there a way to go to the initial cli prompt ??
> 
> -P

From `ri Net::SSH`

```
   Net::SSH.start("host", "user", :password => "password") do |ssh|
     result = ssh.exec!("ls -l")
     puts result
   end

```

Sample of my own stuff…  
require ‘rubygems’  
require ‘net/ssh’

server = “acid”  
user = “jayeola”  
Net::SSH.start(server, user) do |x|  
cmd2 = x.exec!(“test -d /proc && echo "OK"”).strip  
p cmd2  
if cmd2 != “OK”  
p “no proc found”  
else  
p “got proc”  
end

cmd3 = x.exec!(“test -f /proc/partitions && echo "OK"”)  
cmd4 = x.exec!(“test -f /proc/diskstats && echo "OK"”)  
end

I’m using this library and it doesn’t give me a prompt at all but runs  
the code as required. How are you using it?

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [June 2, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/usin-net-ssh-library/139185/3 "2008-06-02T14:11:43Z")

</div>

John M. wrote:

> On Sun, 1 Jun 2008 18:23:25 +0900  
> Prasad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > but  
> > in my case after running the script it goes directly to the shell prompt  
> > “$” by-passing the cli prompt.
> > 
> > a. Why does this behaviour take place??
> > 
> > b. Is there a way to go to the initial cli prompt ??
> > 
> > -P
> 
> From `ri Net::SSH`
> 
> ```
> Net::SSH.start("host", "user", :password => "password") do |ssh|
> result = ssh.exec!("ls -l")
> puts result
> end
> 
> ```
> 
> Sample of my own stuff…  
> require ‘rubygems’  
> require ‘net/ssh’
> 
> server = “acid”  
> user = “jayeola”  
> Net::SSH.start(server, user) do |x|  
> cmd2 = x.exec!(“test -d /proc && echo "OK"”).strip  
> p cmd2  
> if cmd2 != “OK”  
> p “no proc found”  
> else  
> p “got proc”  
> end
> 
> cmd3 = x.exec!(“test -f /proc/partitions && echo "OK"”)  
> cmd4 = x.exec!(“test -f /proc/diskstats && echo "OK"”)  
> end
> 
> I’m using this library and it doesn’t give me a prompt at all but runs  
> the code as required. How are you using it?

Hi John,  
I am using it the same way as you are using. The problem lies  
when the initial prompt that is needed gets by-passed when I use the  
net/ssh module. I have been usng the net/telnet module initially, where  
I get the appropriate prompt on logging in. ie. “\>\>\>”. and the shell  
prompt is “$” which arises when i do a “!” on the initial prompt.

Telnet works fine, but i am exercising the usage of connecting to the  
box thru ssh. So here it by-passes the initial prompt which telnet  
module does not.

As telnet looks for a given prompt while logging in, does ssh have such  
a prompt?

Also I guess the net/ssh module in ruby creates a instance of the shell  
and executed the commands to be executed on that shell rather than  
connecting to the actual machine.

- P

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [June 2, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/usin-net-ssh-library/139185/4 "2008-06-02T14:32:32Z")

</div>

On Mon, 2 Jun 2008 21:11:05 +0900  
Prasad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > b. Is there a way to go to the initial cli prompt ??
> > 
> > if cmd2 != “OK”  
> > I’m using this library and it doesn’t give me a prompt at all but runs  
> > Telnet works fine, but i am exercising the usage of connecting to the
> 
> - P

session.exec!(“some command”) runs that command on the remote server.  
You’ll have to use another method to gain a shell session. Have a look  
at `ri Net::SSH` or use `gem_server` and fire up a browser with  
localhost:8808, (geddit ?). There **is** a way to get a shell but I’ve  
forgotten what it is. Perhaps the gem termios?
