Class: Caracara::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/caracara/ssh.rb

Overview

SSH class

Class Method Summary collapse

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, ssh_options = {})
  "#{(user, host, ssh_options)} -- #{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 (user, host, options = {})
  # Basic command
  command = "ssh #{user}@#{host}"

  # Identity file
  command << " -i #{options[:key]}" unless options[:key].nil?

  # Port
  command << " -p #{options[:port]}" unless options[:port].nil?

  # Forward agent
  command << ' -A' if options[:forward_agent]

  # Check strict host key
  command << ' -o StrictHostKeyChecking=no'

  # Disable pseudo terminal
  command << ' -T'
end