Class: Net::SSH::Session
- Inherits:
-
Object
- Object
- Net::SSH::Session
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#close ⇒ Boolean
Close connection with remote server.
-
#exec(command, &on_output) ⇒ Integer
Execute command.
-
#initialize(host, user, password = '', options = {}) ⇒ Session
constructor
Initialize a new ssh session.
-
#last_command ⇒ SessionCommand
Get last executed command.
- #on_output(&block) ⇒ Object
-
#open(timeout = nil) ⇒ Boolean
Establish connection with remote server.
-
#run(command, options = {}) ⇒ SessionCommand
Execute a single command.
-
#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.
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
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/net/ssh/session.rb', line 23 def initialize(host, user, password='', ={}) @host = host @user = user @password = password @history = [] @track_history = true if [:history] == false @track_history = false end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
13 14 15 |
# File 'lib/net/ssh/session.rb', line 13 def connection @connection end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
16 17 18 |
# File 'lib/net/ssh/session.rb', line 16 def history @history end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/net/ssh/session.rb', line 12 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
15 16 17 |
# File 'lib/net/ssh/session.rb', line 15 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/net/ssh/session.rb', line 14 def end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
12 13 14 |
# File 'lib/net/ssh/session.rb', line 12 def password @password end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
13 14 15 |
# File 'lib/net/ssh/session.rb', line 13 def shell @shell end |
#user ⇒ Object (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
#close ⇒ Boolean
Close connection with remote server
48 49 50 |
# File 'lib/net/ssh/session.rb', line 48 def close shell.close! end |
#exec(command, &on_output) ⇒ Integer
Execute command
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_command ⇒ SessionCommand
Get last executed command
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
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
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, ={}) 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 [: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
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/net/ssh/session.rb', line 108 def run_multiple(commands=[], ={}) results = [] [commands].flatten.compact.each do |cmd| result = run(cmd) yield(result) if block_given? results << result break if results.last.failure? && [:break] == true end results end |