Topic: Playing with Net::SSH

Hi all,

Hope you can possibly help with this... i have been playing with Net::SSH lately and reading the API doing a few things but i am a little confused about how i can exec several commands one after the other...

e.g. in a normal SSH session i would rub something like

cd /var/rails (enter)
rails test_app -d postgresql (enter)

How do i do this with Net:SSH??

I have attempted creating a Block with (Please be nice i'm not overly familiar with this side of Ruby

      channel = ssh.open_channel do |ch|
          ch.exec "cd /var/rails" do |ch, success|
            raise "could not execute command" unless success

            # "on_data" is called when the process writes something to stdout
            ch.on_data do |c, data|
              c.exec "rails test_app -d postgresql"
              #$STDOUT.print data
            ch.on_close { puts "done!" }

Any help in the right direction would be much appreciated. The API doc i am playing with can be found here


Re: Playing with Net::SSH

You could separate commands couple of ways:

# first_command; second_command # <- run commands sequentally
# first_command & second_command # <- run first_command in background, subshell, no wait
# first_command && second_command # <- run second_command if first_command succeeded (exit-status == 0)
# first_command || second_command # <- vice-versa

Re: Playing with Net::SSH

Hi morbusg,

Unfortunately i don't get you... can you please explain a little better and reference to some code that looks like the above i have set out?

Also i found an extra to the Net::SSH its called can be found here.>> this is perfect the only problem is i can't get it to close the session once it starts a rails test_app for example.

Thanks for the reply its much appreciated.


Re: Playing with Net::SSH

jflcooper wrote:

cd /var/rails (enter)
rails test_app -d postgresql (enter)

  ch.exec "cd /var/rails; rails test_app -d postgresql"

I don't know anything about Net::SSH, nor why anyone would need it for this kind of stuff, but that's how you'd separate commands in a shell (in bourne-derived/emulating atleast. SSH comes from 'Secure SHell').