Class: Caracara::SSH
- Inherits:
-
Object
- Object
- Caracara::SSH
- Defined in:
- lib/caracara/ssh.rb
Overview
SSH class
Class Method Summary collapse
-
.command(cmd, escape = true) ⇒ Object
Generate a command.
-
.escape(cmd) ⇒ Object
Escape the command.
-
.exec(cmd) ⇒ Object
Exec the SSH command.
-
.generate(user, host, cmd, ssh_options = {}) ⇒ Object
Generate the SSH command.
-
.login(user, host, options = {}) ⇒ Object
Login command.
Class Method Details
.command(cmd, escape = true) ⇒ Object
Generate a command
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/caracara/ssh.rb', line 37 def command(cmd, escape = true) # Joing commands cmd = cmd.join("\n") if cmd.is_a? Array # Escape the command cmd = escape(cmd) if escape # Return the command cmd end |
.escape(cmd) ⇒ Object
Escape the command
32 33 34 |
# File 'lib/caracara/ssh.rb', line 32 def escape(cmd) Shellwords.escape cmd end |
.exec(cmd) ⇒ Object
Exec the SSH command
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/caracara/ssh.rb', line 54 def exec(cmd) # System the command result = `#{cmd}` # Return the status status = $?.to_i # Return the status `true` for success and `false` for error { result: result, status: !(status.is_a?(Fixnum) && status > 0) } end |
.generate(user, host, cmd, ssh_options = {}) ⇒ Object
Generate the SSH command
49 50 51 |
# File 'lib/caracara/ssh.rb', line 49 def generate(user, host, cmd, = {}) "#{login(user, host, )} -- #{command(cmd)}" end |
.login(user, host, options = {}) ⇒ Object
Login command
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/caracara/ssh.rb', line 11 def login(user, host, = {}) # Basic command command = "ssh #{user}@#{host}" # Identity file command << " -i #{[:key]}" unless [:key].nil? # Port command << " -p #{[:port]}" unless [:port].nil? # Forward agent command << ' -A' if [:forward_agent] # Check strict host key command << ' -o StrictHostKeyChecking=no' # Disable pseudo terminal command << ' -T' end |