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.6'
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.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#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.
-
#method_missing(name, *args) ⇒ Object
Execute a dynamic 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?, #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
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='', ={}) @host = host @user = user @port = [:port] || 22 @password = password @history = [] @track_history = true @timeout = [:timeout] if [: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
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
#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 @options 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 |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
17 18 19 |
# File 'lib/net/ssh/session.rb', line 17 def timeout @timeout 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
55 56 57 |
# File 'lib/net/ssh/session.rb', line 55 def close shell.close! end |
#exec(command, &on_output) ⇒ Integer
Execute command
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_command ⇒ SessionCommand
Get last executed command
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
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
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, ={}) 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 [: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
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/net/ssh/session.rb', line 123 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 |