Class: Rsh

Inherits:
Object
  • Object
show all
Defined in:
lib/rsh.rb

Overview

Ruby remote shell protocol (RFC 1282) client

Creates and operates an rsh client instance. Parameters may be specified through either constructor or attribute accessors. Result of remote command execution (String) is either returned in functional style (execute! method call) or in special attribute, :result.

New: pure ruby rsh client

As of ver. 1.0.0, pure ruby rsh client is implemented. It is used whether if you set :ruby_impl to true or if ‘rsh’ program wasn’t found in the system while setting :ruby_impl to false.

Synopsis

require 'rsh'

rsh = Rsh.new(:host => "c7206", :ruser => "bill",
              :command => "show clock")

rsh.execute! do |line| puts line end

18:30:46.799 MSD Fri Oct 22 2010
  • See c) case in execute! method comment.

Example:

This is an example of rsh in action usin interactive Ruby.

irb(main):001:0> require 'rsh'
=> true

irb(main):002:0> rsh = Rsh.new(:host => "c7206", :ruser => "bill",
                               :command => "show clock")
=> #<Rsh:0x2853f390 @ruser="bill", @executable="/usr/bin/rsh", @result="", @command="show clock", @nullr=false, @host="c7206", @to=3>

irb(main):003:0> rsh.execute! do |line| puts line end

18:30:46.799 MSD Fri Oct 22 2010
=> "\r\n18:30:46.799 MSD Fri Oct 22 2010\n"

See also

% man 1 rsh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Rsh

The Constructor. If :ruby_impl is false checks the presence of rsh in the system (running, naturally, ‘which rsh’) and prepares the command to be run with execute!. rsh CLI arguments are either having default values, being collected from constructor call or specified via accessors.

Call sequence:

Rsh.new #=> Rsh instance
Rsh.new(:host      => "hostname",
        :command   => "example.sh",
        :ruser     => "jack",
        :to        => 5,
        :nullr     => true
        :ruby_impl => true) #=> Rsh instance

If called without arguments, the following default values are being used:

:host      => "localhost"
:command   => ""
:ruser     => "nobody"
:to        => 3
:nullr     => false
:ruby_impl => false
:luser     => ENV["USER"] || "nobody"


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rsh.rb', line 97

def initialize(args={})
  args = {
    :host      => "localhost",
    :command   => "",
    :ruser     => "nobody",
    :luser     => ENV["USER"] || "nobody",
    :to        => 3,
    :nullr     => false,
    :ruby_impl => false
  }.merge!(args)

  @result     = ""
  @ruby_impl  = args[:ruby_impl]
  @host       = args[:host]
  @command    = args[:command]
  @ruser      = args[:ruser]
  @luser      = args[:luser]
  @to         = args[:to]
  @nullr      = args[:nullr]

  begin
    open("| which rsh") do |io|
      @executable = io.gets.chomp
    end
  rescue => detail
    @ruby_impl = true
  end
end

Instance Attribute Details

#commandObject

remote command, String



62
63
64
# File 'lib/rsh.rb', line 62

def command
  @command
end

#executableObject (readonly)

path to rsh program, String



56
57
58
# File 'lib/rsh.rb', line 56

def executable
  @executable
end

#hostObject

remote server hostname or IP, String



60
61
62
# File 'lib/rsh.rb', line 60

def host
  @host
end

#luserObject

Local user name, used in pure ruby implementation.



66
67
68
# File 'lib/rsh.rb', line 66

def luser
  @luser
end

#nullrObject

boolean knob for /dev/null redirection; see man rsh for further information



71
72
73
# File 'lib/rsh.rb', line 71

def nullr
  @nullr
end

#resultObject (readonly)

result String



58
59
60
# File 'lib/rsh.rb', line 58

def result
  @result
end

#ruby_implObject

flags whether to force pure ruby rsh client



54
55
56
# File 'lib/rsh.rb', line 54

def ruby_impl
  @ruby_impl
end

#ruserObject

remote username, String



64
65
66
# File 'lib/rsh.rb', line 64

def ruser
  @ruser
end

#toObject

rsh timeout, Integer (see man 1 rsh)



68
69
70
# File 'lib/rsh.rb', line 68

def to
  @to
end

Instance Method Details

#execute!(command = nil) ⇒ Object Also known as: execute

Executes rsh command using previously collected arguments. If :ruby_impl is true or system rsh binary was not found during initialize runs pure ruby rsh client.

If given a block, calls it for each line received from rsh output (parameter line).

Call sequence:

a)

rsh_inst.execute! do |line|
  puts line
done #=> complete_result_String

b)

rsh_inst.execute! #=> complete_result_String

c)

rsh_inst.execute! "remote_command" #=> same as above, but replacing
                                       @command with the given string
Returns

the complete rsh output as one String. The result is also stored and available via result attribute.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rsh.rb', line 154

def execute!(command=nil)
  @command = command if command
  @result = ""
  if @ruby_impl
    # pure ruby implementation call
    rsh_ruby do |line|
      yield(line) if block_given?
      @result << line
    end
  else
    # OS'es rsh(1) call
    open "|#{@executable} #{"-n" if @nullr} -l#{@ruser} -t#{@to} #{@host} #{@command}" do |io|
      io.each do |line|
        yield(line) if block_given?
        @result << line
      end
    end
  end
  @result
end