1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Net:SSH--Executing Programs with Net::SSH

Run Programs With Net:SSH

By

Undoubtedly, one of the things you'll want to be able to do with Net::SSH is to run programs. The Session object gives you two convenient methods with which to accomplish this task: exec and exec!.

Behind the scenes, these methods set up channels, call exec on the channel and set up some callback handlers. When you call them, you can pass a block that will be called when new data arrives from the remote host. If you do not supply a block, any output will simply be written to $stdout.

The simplest form of this method was seen in one of the previous Net::SSH examples. The ls command is run and its output is displayed on the screen.

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
  ssh.exec('ls')
end

Pass a Block For More Control

This doesn't, however, give you much control. It just executes the program and prints whatever is returned. If you want more control and to be able to do something with the output, pass a block to the exec method. The block takes 3 arguments: the channel, the stream and the data. The channel is the channel created by the session. The stream is a symbol, (either :stdout or :stderr) to signify the origin of the output. The data is a string containing the data.

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
  ssh.exec('ls') do|ch, stream, data|
    puts "Got this on the #{stream} stream: "
    puts data
  end
end

A Block Variant

The second variant of this method, exec!, is even easier to use. When you call exec, the method returns immediately, leaving the block to do all the work. When you call exec!, it will wait until the program on the remote host finishes executing before returning. Further, if no block is passed to it, exec! will return all data output by the program on the remote host. This makes it ideal for running a quick program and capturing the output in a string.

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
  output = ssh.exec!('ls')
  puts output
end

However, the behavior of the exec method is beneficial as well. It allows you to run several programs at the same time. Calling exec in succession will tell the remote machine to run all of the programs at the same time. This can be much faster depending on the programs and depending on how fast the remote machine is. And of course, this isn't very useful for trivial commands like ls used in these examples, but rather commands that take a long period of time to complete.

  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. SSH
  6. Net:SSH--Executing Programs with Net::SSH

©2014 About.com. All rights reserved.