Documentation & tutorials for net::ssh

Hi

I'm wondering if anyone can recommend any guides & tutorials for using
net:ssh, apart from what's on the main website for net::ssh.

Failing that information on any books that cover net::ssh as well as Ruby
would be greatly appriciated.

I have some difficulties with something I'm trying to do, but would rather
rtfm if I can help it, in case my problems are really basic.

bob

I'm wondering if anyone can recommend any guides & tutorials for using
net:ssh, apart from what's on the main website for net::ssh.

Failing that information on any books that cover net::ssh as well as Ruby
would be greatly appriciated.

I'm afraid the only online resource I'm aware of is http://net-ssh.rubyforge.org, and if anyone has written any books about Net::SSH, it's a real surprise to me. :slight_smile:

I have some difficulties with something I'm trying to do, but would rather
rtfm if I can help it, in case my problems are really basic.

Feel free to ask. If you've read http://net-ssh.rubyforge.org, then you've read plenty of manual and deserve a few questions to the list.

Or feel free email me directly. (I'm the author of Net::SSH.)

- Jamis

···

On Oct 6, 2005, at 3:41 PM, bob wrote:

Excellent! I did try emailing an address on the net::ssh website but
it bounced. I came here after that to have a nosey around after neglecting
to use usenet for more than binary downloads for ages. I'll have a bit
more of a read and then come back if I am still stuck. I should add that
I'm also new to ruby & object orientated coding generally, which doesn't
help matters, but I'm a fast learner.

···

On Fri, 07 Oct 2005 06:49:01 +0900, Jamis Buck wrote:

On Oct 6, 2005, at 3:41 PM, bob wrote:

Feel free to ask. If you've read http://net-ssh.rubyforge.org, then
you've read plenty of manual and deserve a few questions to the list.

Or feel free email me directly. (I'm the author of Net::SSH.)

- Jamis

why does this script continue looping and not end gracefully with the
output I want. I can't figure out where to put channel.close :frowning:

#!/usr/local/bin/ruby

require 'rubygems'
require 'net/ssh'
require "cgi"

def do_tail( session, file )
  session.open_channel do |channel|
    channel.on_data do |ch, data|
      puts "[#{file}] -> #{data}"
    end
    channel.exec "tail -f #{file}"
  end
end

session = Net::SSH.start( 'servername', 'username', 'password' ) do

session>

  do_tail session, "/var/log/messages"
  session.loop
end

This is 99.99% example script from the net::ssh site I know, but I have
a modified version that I want to expand and use but it's driving me
mad trying to work out how to make the loop stop when the job is done.

session.loop { some_condition_that_should_return_true_while_work_is_being_done }

See the last paragraph of http://net-ssh.rubyforge.org/chapter-3.html#s2\. Basically, session.loop accepts an optional block that should return true as long as the loop should continue.

Normally, the loop continues while all channels are open, but in your case, a tail -f will never close by itself.

- Jamis

···

On Oct 10, 2005, at 10:26 AM, hack0r@gmail.com wrote:

why does this script continue looping and not end gracefully with the
output I want. I can't figure out where to put channel.close :frowning:

#!/usr/local/bin/ruby

require 'rubygems'
require 'net/ssh'
require "cgi"

def do_tail( session, file )
  session.open_channel do |channel|
    channel.on_data do |ch, data|
      puts "[#{file}] -> #{data}"
    end
    channel.exec "tail -f #{file}"
  end
end

session = Net::SSH.start( 'servername', 'username', 'password' ) do
>session>
  do_tail session, "/var/log/messages"
  session.loop
end

See the last paragraph of http://net-ssh.rubyforge.org/
chapter-3.html#s2. Basically, session.loop accepts an optional block
that should return true as long as the loop should continue.

Normally, the loop continues while all channels are open, but in your
case, a tail -f will never close by itself.

- Jamis

That's as much as I had worked out. Reading the guide suggested to me that
it wasn't ending because the channel was still open, so I started trying
to use channel.close in various places to make it close, but it never did
what I wanted it to do. I did get it to close & end the script, but
only before the result of "tail /var/log/messages" was output to the shell
(or browser, when run as a cgi script). I didn't know where to begin,
when it came to adding an optional block of code, and didn't start because
I wasn't 100% that was what I needed to do.

Any clues anyone, please ? I'm going to have a go at working out what I
should do, but would greatly appreciate the answer from
someone who knows :slight_smile:

Can you post an example of one of the scripts where you were doing channel.close? Under what conditions do you want the channel to close?

- Jamis

···

On Oct 10, 2005, at 1:01 PM, bob wrote:

See the last paragraph of http://net-ssh.rubyforge.org/
chapter-3.html#s2. Basically, session.loop accepts an optional block
that should return true as long as the loop should continue.

Normally, the loop continues while all channels are open, but in your
case, a tail -f will never close by itself.

- Jamis

That's as much as I had worked out. Reading the guide suggested to me that
it wasn't ending because the channel was still open, so I started trying
to use channel.close in various places to make it close, but it never did
what I wanted it to do. I did get it to close & end the script, but
only before the result of "tail /var/log/messages" was output to the shell
(or browser, when run as a cgi script). I didn't know where to begin,
when it came to adding an optional block of code, and didn't start because
I wasn't 100% that was what I needed to do.

Any clues anyone, please ? I'm going to have a go at working out what I
should do, but would greatly appreciate the answer from
someone who knows :slight_smile:

None of the scripts I am trying to write are earth shattering. They (will
eventually) simply consist of running a script or a few commands piped
together on a remote server, grabbing & parsing the output to get some
variables, then building a web page containing those variables. None of
the scripts I want to get done have gone beyond dumping the output I want
to the shell at this point in time, because I want to know how to close
that ssh connection. I think just knowing what I need to do to the script
already given, to make it end gracefully, would solve my problem.

···

On Tue, 11 Oct 2005 04:08:35 +0900, Jamis Buck wrote:

On Oct 10, 2005, at 1:01 PM, bob wrote:

See the last paragraph of http://net-ssh.rubyforge.org/
chapter-3.html#s2. Basically, session.loop accepts an optional block
that should return true as long as the loop should continue.

Normally, the loop continues while all channels are open, but in your
case, a tail -f will never close by itself.

- Jamis

That's as much as I had worked out. Reading the guide suggested to
me that
it wasn't ending because the channel was still open, so I started
trying
to use channel.close in various places to make it close, but it
never did
what I wanted it to do. I did get it to close & end the script, but
only before the result of "tail /var/log/messages" was output to
the shell
(or browser, when run as a cgi script). I didn't know where to begin,
when it came to adding an optional block of code, and didn't start
because
I wasn't 100% that was what I needed to do.

Any clues anyone, please ? I'm going to have a go at working out
what I
should do, but would greatly appreciate the answer from
someone who knows :slight_smile:

Can you post an example of one of the scripts where you were doing
channel.close? Under what conditions do you want the channel to close?

- Jamis

Bob,

Let me ask it this way. What shell command are you giving to the remote server? Are the commands self-terminating, or do they remain open waiting for a signal or key-press or whatever?

- Jamis

···

On Oct 10, 2005, at 1:51 PM, bob wrote:

On Tue, 11 Oct 2005 04:08:35 +0900, Jamis Buck wrote:

Can you post an example of one of the scripts where you were doing
channel.close? Under what conditions do you want the channel to close?

- Jamis

None of the scripts I am trying to write are earth shattering. They (will
eventually) simply consist of running a script or a few commands piped
together on a remote server, grabbing & parsing the output to get some
variables, then building a web page containing those variables. None of
the scripts I want to get done have gone beyond dumping the output I want
to the shell at this point in time, because I want to know how to close
that ssh connection. I think just knowing what I need to do to the script
already given, to make it end gracefully, would solve my problem.

Bob,

Let me ask it this way. What shell command are you giving to the
remote server? Are the commands self-terminating, or do they remain
open waiting for a signal or key-press or whatever?

- Jamis

They are all going to be self terminating, like the example I gave.

Actually, the example you gave was "tail -f #{file}", and tail -f is not self-terminating. It remains open until the command is killed (TERM signal, for instance), outputting lines from the end of the file as they appear.

If you want it to be self-terminating, try removing the -f switch. Let me know if that helps.

- Jamis

···

On Oct 10, 2005, at 2:11 PM, bob wrote:

Bob,

Let me ask it this way. What shell command are you giving to the
remote server? Are the commands self-terminating, or do they remain
open waiting for a signal or key-press or whatever?

- Jamis

They are all going to be self terminating, like the example I gave.

ooops :s I'd started with trying to modify a different
example, which I had trouble with due to a lack of understanding of that
one along with a feeling that it was perhaps inappropriate, so I moved
onto this one as it seemed simpler. I was too busy trying to make the
example end gracefully that I was paying too much attention to ruby & not
enough to what was being executed on the remote server in the example.

Thanks :slight_smile:

···

On Tue, 11 Oct 2005 05:19:30 +0900, Jamis Buck wrote:

Actually, the example you gave was "tail -f #{file}", and tail -f is
not self-terminating. It remains open until the command is killed
(TERM signal, for instance), outputting lines from the end of the
file as they appear.

If you want it to be self-terminating, try removing the -f switch.
Let me know if that helps.

- Jamis