Class: Hydra::SSH

Inherits:
Object
  • Object
show all
Includes:
MessagingIO, Open3
Defined in:
lib/hydra/ssh.rb

Overview

Read and write with an ssh connection. For example:

@ssh = Hydra::SSH.new(
  'localhost', # connect to this machine
  '/home/user', # move to the home directory
  "ruby hydra/test/echo_the_dolphin.rb" # run the echo script
)
@message = Hydra::Messages::TestMessage.new("Hey there!")
@ssh.write @message
puts @ssh.gets.text
  => "Hey there!"

Note that what ever process you run should respond with Hydra messages.

Instance Method Summary collapse

Methods included from MessagingIO

#gets, #write

Constructor Details

#initialize(connection_options, directory, command) ⇒ SSH

Initialize new SSH connection. The first parameter is passed directly to ssh for starting a connection. The second parameter is the directory to CD into once connected. The third parameter is the command to run So you can do:

Hydra::SSH.new('-p 3022 [email protected]', '/home/user/Desktop', 'ls -l')

To connect to server.com as user on port 3022, then CD to their desktop, then list all the files.



28
29
30
31
32
33
# File 'lib/hydra/ssh.rb', line 28

def initialize(connection_options, directory, command)
  @writer, @reader, @error = popen3("ssh -tt #{connection_options}")
  @writer.write("mkdir -p #{directory}\n")
  @writer.write("cd #{directory}\n")
  @writer.write(command+"\n")
end

Instance Method Details

#closeObject

Close the SSH connection



36
37
38
39
# File 'lib/hydra/ssh.rb', line 36

def close
  @writer.write "exit\n"
  super
end