Executing Remote Shell Script Using Net::SSH

One of the server at work is using SVN to update the site content. Why not Capistrano? Because the app is written in PHP. OK, shoot me, I know…

I really wanted to use Ruby to write the script. So, here it is. You’ll be amazed how small the script really is.


#!/usr/bin/env ruby -w

require 'rubygems'  # --> I need this for Mac OS X or next line doesn't work.
require 'net/ssh'

Net::SSH.start('the_host_name', :username => 'user_name',
                :password => 'whatever_the_password') do |session|
puts "Successfully connected, now creating the synchronous shell..."
shell = session.shell.sync
puts "Sending the command..."
shell.send_command '/{script_directory_outside_web} && siteupdate'
puts "Done!"
end

The content of the “siteupdate” is simple “cd {web_directory} | svn update” command.

Comments

2 Responses to “Executing Remote Shell Script Using Net::SSH”

  1. unbewusst on July 27th, 2008 4:19 am

    when using :
    session=Net::SSH.start(“TT”, “root”, options)
    shell=session.shell.sync
    out=shell.send_command ‘ls -lart’
    puts out.stdout

    i get :
    $ ./ssh_shell_sync.rb
    ./ssh_shell_sync.rb:13: undefined method `shell’ for # (NoMethodError)

    Im using Mac OS X 10.4.11
    $ ruby -v
    ruby 1.8.6 (2007-09-24 patchlevel 111) [powerpc-darwin8.11.0]
    needle-1.3.0.gem
    net-ssh-2.0.3.gem

    did u use a special option when installing net-ssh in order to enable “needle” ???

  2. Joon on July 27th, 2008 12:33 pm

    You have to pass in the block.

Powered by Olark