Class: Net::SSH::Session

Inherits:
Object
  • Object
show all
Includes:
SessionHelpers
Defined in:
lib/net/ssh/session.rb,
lib/net-ssh-session/version.rb

Constant Summary collapse

VERSION =
'0.1.6'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SessionHelpers

#capture, #chdir, #directory_exists?, #env, #export, #export_hash, #file_exists?, #has_group?, #has_user?, #kill_process, #last_exit_code, #process_exists?, #pwd, #read_file, #sudo, #symlink_exists?, #with_timeout

Constructor Details

#initialize(host, user, password = '', options = {}) ⇒ Session

Initialize a new ssh session

Parameters:

  • remote (String)

    hostname or ip address

  • remote (String)

    account username

  • remote (String)

    account password

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

    hash



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/net/ssh/session.rb', line 24

def initialize(host, user, password='', options={})
  @host          = host
  @user          = user
  @port          = options[:port] || 22
  @password      = password
  @history       = []
  @track_history = true
  @timeout       = options[:timeout]

  if options[:history] == false
    @track_history = false
  end

  if @timeout && !@timeout.kind_of?(Integer)
    raise ArgumentError, "Timeout value should be numeric"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Execute a dynamic command

Parameters:

  • command (String)

    name



151
152
153
# File 'lib/net/ssh/session.rb', line 151

def method_missing(name, *args)
  run("#{name} #{args.join(' ')}".strip)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/net/ssh/session.rb', line 13

def connection
  @connection
end

#historyObject (readonly)

Returns the value of attribute history.



16
17
18
# File 'lib/net/ssh/session.rb', line 16

def history
  @history
end

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/net/ssh/session.rb', line 12

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/net/ssh/session.rb', line 15

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/net/ssh/session.rb', line 14

def options
  @options
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/net/ssh/session.rb', line 12

def password
  @password
end

#shellObject (readonly)

Returns the value of attribute shell.



13
14
15
# File 'lib/net/ssh/session.rb', line 13

def shell
  @shell
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/net/ssh/session.rb', line 17

def timeout
  @timeout
end

#userObject (readonly)

Returns the value of attribute user.



12
13
14
# File 'lib/net/ssh/session.rb', line 12

def user
  @user
end

Instance Method Details

#closeBoolean

Close connection with remote server

Returns:

  • (Boolean)


55
56
57
# File 'lib/net/ssh/session.rb', line 55

def close
  shell.close!
end

#exec(command, &on_output) ⇒ Integer

Execute command

Parameters:

  • command (String)

    to execute

  • output (Block)

    event block

Returns:

  • (Integer)

    command execution exit code



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/net/ssh/session.rb', line 67

def exec(command, &on_output)
  status = nil

  handle_timeout do
    shell.execute(command) do |process|
      process.on_output(&on_output)
      process.on_error_output(&on_output)
      process.on_finish { |p| status = p.exit_status }
    end

    shell.session.loop(1) { status.nil? }
  end

  status
end

#last_commandSessionCommand

Get last executed command

Returns:



144
145
146
# File 'lib/net/ssh/session.rb', line 144

def last_command
  history.last
end

#on_output(&block) ⇒ Object



59
60
61
# File 'lib/net/ssh/session.rb', line 59

def on_output(&block)
  @on_output = block
end

#open(timeout = nil) ⇒ Boolean

Establish connection with remote server

Parameters:

  • max (Integer)

    timeout in seconds

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/net/ssh/session.rb', line 45

def open(timeout=nil)
  if timeout && timeout > 0
    with_timeout(timeout) { establish_connection }
  else
    establish_connection
  end
end

#run(command, options = {}) ⇒ SessionCommand

Execute a single command

Parameters:

  • comand (String)

    to execute

  • execution (Hash)

    options

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/net/ssh/session.rb', line 87

def run(command, options={})
  output  = ''
  t_start = Time.now

  exit_code = exec(command) do |process, data|
    output << data
    yield data if block_given?
  end

  t_end = Time.now

  cmd = SessionCommand.new(
    command, output, exit_code, 
    t_end - t_start
  )

  cmd.start_time = t_start
  cmd.finish_time = t_end

  if options[:history] == true || @track_history == true
    history << cmd
  end

  logger.info(cmd.to_s) if logger

  cmd
end

#run_multiple(commands = [], options = {}) ⇒ Array

Execute multiple commands Execution options are the following: options - If set to ‘true`, execution chain will break on first failed command

Parameters:

  • set (Array)

    of commands to execute

  • execution (Hash)

    options

Returns:

  • (Array)

    set of command execution results



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/net/ssh/session.rb', line 123

def run_multiple(commands=[], options={})
  results = []

  [commands].flatten.compact.each do |cmd|
    result = run(cmd)
    yield(result) if block_given?
    results << result
    break if results.last.failure? && options[:break] == true
  end

  results
end