Class: Channelizer::Channels::Ssh

Inherits:
Base
  • Object
show all
Defined in:
lib/channelizer/channels/ssh.rb

Instance Method Summary collapse

Methods inherited from Base

#ready?, required_option, #shell_execute, validate_options

Methods included from Util::Retryable

#retryable

Constructor Details

#initialize(options) ⇒ Ssh

Creates a SSH Session

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :username (String)

    the username

  • :password (String)

    the password

  • :host (String)

    the remote host

  • :port (String) — default: 5985

    the port to connect on

  • :keys (String)

    the keys to try logging in with



18
19
20
21
22
23
24
25
26
27
# File 'lib/channelizer/channels/ssh.rb', line 18

def initialize(options)
  defaults = { :port => 22, :paranoid => false }
  options = defaults.merge(options)

  # Options need to be in a final state before we call the parent initializer
  super(options)

  @connection_options = options

end

Instance Method Details

#execute(command, options = { }) ⇒ Fixnum

Executes a command on the channel

Parameters:

  • command

    the command to execute on the channel

Returns:

  • (Fixnum)

    the exit code of the command

Raises:

  • (StandardException)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/channelizer/channels/ssh.rb', line 41

def execute(command, options = { })
  defaults = { :out_console => true }
  options = defaults.merge(options)
  # open a new channel and configure a minimal set of callbacks, then run
  # the event loop until the channel finishes (closes)
  last_exit = -1
  channel = session.open_channel do |ch|

    #request pty for sudo stuff and so
    ch.request_pty do |ch, success|
      raise "Error requesting pty" unless success
    end
      
    ch.exec "#{command}" do |ch, success|
      raise "could not execute command" unless success


      # "on_data" is called when the process writes something to stdout
      ch.on_data do |c, data|
        STDOUT.print data if options[:out_console]

      end

      # "on_extended_data" is called when the process writes something to stderr
      ch.on_extended_data do |c, type, data|
        STDOUT.print data if options[:out_console]
      end

      channel.on_request("exit-signal") do |ch, data|
        last_exit = data.read_long
      end

      channel.on_request("exit-status") do |ch,data|
        last_exit = data.read_long
      end

    end
  end
  channel.wait
  last_exit
end

#sessionObject



29
30
31
32
33
34
# File 'lib/channelizer/channels/ssh.rb', line 29

def session
  options = @connection_options.clone
  host = options.delete(:host)
  username = options.delete(:username)
  Net::SSH.start(host,username,options)
end

#sudo_command(command) ⇒ String

Echos the command (Windows does not support sudo)

Parameters:

  • command (String)

    the command to wrap in a sudo context

Returns:

  • (String)

    the command that has been wrapped in a sudo context



87
88
89
# File 'lib/channelizer/channels/ssh.rb', line 87

def sudo_command(command)
  "sudo #{command}"
end

#upload(source, destination, options = {}) ⇒ Fixnum

Uploads a file over WinRM

Parameters:

  • source

    the file to upload

  • destination

    where to place the file on the remote host

Returns:

  • (Fixnum)


96
97
98
99
100
101
102
# File 'lib/channelizer/channels/ssh.rb', line 96

def upload(source, destination, options = {} )
  options = @connection_options.clone
  host = options.delete(:host)
  username = options.delete(:username)
  channel = session.scp.upload(source,destination)
  channel.wait
end