Module: Open4ssh
- Defined in:
- lib/open4ssh.rb,
lib/open4ssh/version.rb
Overview
Open4ssh is a small convenience wrapper for net-ssh. Its intended and primary purpose is to provide pragmatic execution of shell commands on a remote host via SSH.
It provides the following functions:
-
Open4ssh.capture to execute one command on a remote host via SSH and get the console output back.
-
Open4ssh.capture3 to execute one command on a remote host via SSH and get the exit code, standard out, standard error of the command back.
-
Open4ssh.capture4 to execute a sequence of commands on a remote host via SSH and get all return values (exit code, standard out, standard error, command) back.
-
Open4ssh.success to evaluate whether a sequence of commands was successful.
-
Open4ssh.stdout to get all standard out messages of a sequence of commands.
-
Open4ssh.stderr to get all standard error messages of a sequence of commands.
-
Open4ssh.console to get all console output (union of standard out and standard error messages) of a sequence of commands.
Constant Summary collapse
- VERSION =
Version of Open4ssh according to semantic versioning
"0.1.4"
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.
-
.capture3(host: '', user: '', port: 22, key: '', pwd: '', cmd: '', verbose: false) ⇒ exit_code, ...
Executes one shell command on a remote host via SSH and captures it exit code, stdout and stderr.
-
.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.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/open4ssh.rb', line 50 def self.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '') stdout = "" keys = [key] Net::SSH.start(host, user, port: port, password: pwd, keys: keys) do |ssh| stdout = ssh.exec!(cmd) end return stdout end |
.capture3(host: '', user: '', port: 22, key: '', pwd: '', cmd: '', verbose: false) ⇒ exit_code, ...
Executes one shell command on a remote host via SSH and captures it exit code, stdout and stderr.
81 82 83 84 85 86 87 |
# File 'lib/open4ssh.rb', line 81 def self.capture3(host: '', user: '', port: 22, key: '', pwd: '', cmd: '', verbose: false) returns = self.capture4(host: host, user: user, port: port, key: key, pwd: pwd, cmd: [cmd], verbose: verbose) exit_code = returns.last[0] std_out = returns.last[1] std_err = returns.last[2] return exit_code, std_out, std_err 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).
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/open4ssh.rb', line 115 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.
267 268 269 270 271 |
# File 'lib/open4ssh.rb', line 267 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.
240 241 242 243 244 |
# File 'lib/open4ssh.rb', line 240 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.
210 211 212 213 214 |
# File 'lib/open4ssh.rb', line 210 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.
181 182 183 184 |
# File 'lib/open4ssh.rb', line 181 def self.success(results) results.select { |result| result[0] != 0 } .empty? end |