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.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SessionHelpers

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

Constructor Details

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

Initialize a new ssh session

Parameters:

  • hostname or ip address

  • account username

  • account password

  • (defaults to: {})

    hash



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/net/ssh/session.rb', line 23

def initialize(host, user, password='', options={})
  @host          = host
  @user          = user
  @password      = password
  @history       = []
  @track_history = true

  if options[:history] == false
    @track_history = false
  end
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

#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:



48
49
50
# File 'lib/net/ssh/session.rb', line 48

def close
  shell.close!
end

#exec(command, &on_output) ⇒ Integer

Execute command

Parameters:

  • to execute

  • event block

Returns:

  • command execution exit code



60
61
62
63
64
65
66
67
68
69
# File 'lib/net/ssh/session.rb', line 60

def exec(command, &on_output)
  status = nil
  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? }
  status
end

#last_commandSessionCommand

Get last executed command

Returns:



129
130
131
# File 'lib/net/ssh/session.rb', line 129

def last_command
  history.last
end

#on_output(&block) ⇒ Object



52
53
54
# File 'lib/net/ssh/session.rb', line 52

def on_output(&block)
  @on_output = block
end

#open(timeout = nil) ⇒ Boolean

Establish connection with remote server

Parameters:

  • timeout in seconds

Returns:



38
39
40
41
42
43
44
# File 'lib/net/ssh/session.rb', line 38

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:

  • to execute

  • options

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/net/ssh/session.rb', line 75

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
  )

  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:

  • of commands to execute

  • options

Returns:

  • set of command execution results



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/net/ssh/session.rb', line 108

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