Module: Open4ssh
- Defined in:
- lib/open4ssh.rb,
lib/open4ssh/version.rb
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
-
.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '') ⇒ String
Executes a shell command on a remote host via SSH and captures the console output.
-
.capture4(host: '', user: '', port: 22, key: '', pwd: '', cmd: [], verbose: false) ⇒ Array<exit_code, std_out, std_err, command>
Executes a list of shell commands on a remote host via SSH and captures their exit codes, stdouts and stderrs.
-
.console(results) ⇒ String
Collects all console messages (stdout + stderr) of a list of executed shell commands.
-
.stderr(results) ⇒ String
Collects all stderr messages of a list of executed shell commands.
-
.stdout(results) ⇒ String
Collects all stdout messages of a list of executed shell commands.
-
.success(results) ⇒ Bool
Determines whether a list of shell commands has been executed successfully.
Class Method Details
.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '') ⇒ String
Executes a shell command on a remote host via SSH and captures the console output.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/open4ssh.rb', line 26 def self.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '') stdout = "" keys = [key] Net::SSH.start(host, user, port: 22, password: pwd, keys: keys) do |ssh| result = ssh.exec!(cmd) stdout = result end return stdout end |
.capture4(host: '', user: '', port: 22, key: '', pwd: '', cmd: [], verbose: false) ⇒ Array<exit_code, std_out, std_err, command>
Executes a list of shell commands on a remote host via SSH and captures their exit codes, stdouts and stderrs. The commands are executed sequentially until a command terminates with an exit code not equal 0 (no success).
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/open4ssh.rb', line 64 def self.capture4(host: '', user: '', port: 22, key: '', pwd: '', cmd: [], verbose: false) keys = [key] results = [] Net::SSH.start(host, user, port: port, password: pwd, keys: keys) do |ssh| # Execute command by command for command in cmd stdout = "" stderr = "" code = nil channel = ssh.open_channel do |ch| ch.exec(command) do |c, success| c.close unless success c.on_data do |_, data| stdout += data $stdout.puts(data) if verbose end c.on_extended_data do |_, _, data| stderr += data $stderr.puts(data) if verbose end c.on_request('exit-status') { |_, data| code = data.read_long } end end channel.wait results << [code, stdout, stderr, command] # If last command was not successful stop execution if code != 0 ssh.close return results end end end return results end |
.console(results) ⇒ String
Collects all console messages (stdout + stderr) of a list of executed shell commands.
216 217 218 219 220 |
# File 'lib/open4ssh.rb', line 216 def self.console(results) output = results.map { |result| "#{result[1]}\n#{result[2]}" } .select { |console| not console.strip.empty? } * "\n" output.strip end |
.stderr(results) ⇒ String
Collects all stderr messages of a list of executed shell commands.
189 190 191 192 193 |
# File 'lib/open4ssh.rb', line 189 def self.stderr(results) output = results.map { |result| result[2] } .select { |stderr| not stderr.strip.empty? } * "\n" output.strip end |
.stdout(results) ⇒ String
Collects all stdout messages of a list of executed shell commands.
159 160 161 162 163 |
# File 'lib/open4ssh.rb', line 159 def self.stdout(results) output = results.map { |result| result[1] } .select { |stdout| not stdout.strip.empty? } * "\n" output.strip end |
.success(results) ⇒ Bool
Determines whether a list of shell commands has been executed successfully.
130 131 132 133 |
# File 'lib/open4ssh.rb', line 130 def self.success(results) results.select { |result| result[0] != 0 } .empty? end |