Class: RemoteExec::Ssh

Inherits:
Base
  • Object
show all
Defined in:
lib/remote-exec/ssh.rb

Overview

Class to help establish SSH connections, issue remote commands, and transfer files between a local system and remote node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, username, options = {}) {|self| ... } ⇒ Ssh

Constructs a new Ssh object.

Parameters:

  • hostname (String)

    the remote hostname (IP address, FQDN, etc.)

  • username (String)

    the username for the remote host

  • options (Hash) (defaults to: {})

    configuration options for ssh

Yields:

  • (self)

    if a block is given then the constructed object yields itself and calls #shutdown at the end, closing the remote connection



31
32
33
34
35
36
# File 'lib/remote-exec/ssh.rb', line 31

def initialize(hostname, username, options = {})
  @hostname = hostname
  @username = username
  @options = options
  super()
end

Instance Attribute Details

#hostnameObject (readonly)

hostname for the connection



17
18
19
# File 'lib/remote-exec/ssh.rb', line 17

def hostname
  @hostname
end

#optionsObject

options for the connection



21
22
23
# File 'lib/remote-exec/ssh.rb', line 21

def options
  @options
end

#usernameObject (readonly)

username for the connection



19
20
21
# File 'lib/remote-exec/ssh.rb', line 19

def username
  @username
end

Instance Method Details

#execute(command) ⇒ Integer

Execute command on remote host

Parameters:

  • command (String)

    command string to execute

Returns:

  • (Integer)

    exit status of the command



57
58
59
60
61
62
63
64
# File 'lib/remote-exec/ssh.rb', line 57

def execute(command)
  # TODO: make it run in one session
  @last_status = nil
  @command     = command
  session.open_channel(&method(:execute_open_channel))
  session.loop
  @last_status
end

#shutdownObject

Shuts down the session connection, if it is still active.



43
44
45
46
47
48
49
# File 'lib/remote-exec/ssh.rb', line 43

def shutdown
  super
  return if @session.nil?
  session.shutdown!
ensure
  @session = nil
end

#to_sObject



38
39
40
# File 'lib/remote-exec/ssh.rb', line 38

def to_s
  "<RemoteExec::Ssh @hostname=#{@hostname.inspect}, @username=#{@username.inspect}>"
end