Class: SimpleSsh::SimpleSSH

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SimpleSSH

class methods end



16
17
18
19
20
21
22
# File 'lib/simple_ssh.rb', line 16

def initialize(options={})
  @hosts = options[:hosts] ||  []
  @cmds  = options[:cmds] || []
  @keys = options[:keys] || []
  @user = options[:user] || ""
  @result_by_host = {}
end

Instance Attribute Details

#result_by_hostObject (readonly)

Returns the value of attribute result_by_host.



25
26
27
# File 'lib/simple_ssh.rb', line 25

def result_by_host
  @result_by_host
end

#userObject

Returns the value of attribute user.



24
25
26
# File 'lib/simple_ssh.rb', line 24

def user
  @user
end

Class Method Details

.configure(options = {}) {|ssh| ... } ⇒ Object

Yields:

  • (ssh)


9
10
11
12
13
# File 'lib/simple_ssh.rb', line 9

def configure(options={})
  ssh = SimpleSsh::SimpleSSH.new(options)
  yield(ssh) if block_given?
  ssh
end

Instance Method Details

#cmdsObject



31
32
33
# File 'lib/simple_ssh.rb', line 31

def cmds
  @cmds ||= []
end

#executeObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple_ssh.rb', line 39

def execute
  if block_given?
    @cmds.clear
    yield(self)
  end

  results = {}
  hosts.each do |host|
    @result_by_host[host] = execute_cmds_on(@user, host)
  end
  result_by_host
end

#hostsObject



27
28
29
# File 'lib/simple_ssh.rb', line 27

def hosts
  @hosts ||= []
end

#keysObject



35
36
37
# File 'lib/simple_ssh.rb', line 35

def keys
  @keys ||= []
end

#result_by_cmdObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_ssh.rb', line 52

def result_by_cmd
  result = {}
  result_by_host.each do |host, cmd_dict|
    cmd_dict.each do |cmd, res|
      result[cmd] ||= {}
      result[cmd][host]= res
    end
  end
  result
end

#to_csv(sep = ",") ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/simple_ssh.rb', line 79

def to_csv(sep=",")
  require "csv" unless defined? CSV
  csv = []

  cols = ["host", "cmd", "result"]
  csv << cols.join(sep)

  result_by_host.each do |host, cmd_dict|
    cmd_dict.each do |cmd, res|
      csv << (host + sep + cmd + sep + cmd_dict[cmd])
    end
  end

  csv.join("\n")
end

#to_sObject



63
64
65
66
67
68
69
70
71
# File 'lib/simple_ssh.rb', line 63

def to_s
  result_by_host.map do |host, cmd_dict|
    res = "#{host}:\n"
    cmd_dict.each do |cmd,cmd_res|
      res << "\t#{cmd}: #{cmd_res.split("\n").join("\n\t\t")}\n"
    end
    res
  end.join("")
end

#to_yaml(by = :host) ⇒ Object



73
74
75
76
77
# File 'lib/simple_ssh.rb', line 73

def to_yaml(by=:host)
  require 'yaml' unless defined? YAML
  s = send "result_by_#{by.to_s}".to_sym
  YAML.dump(s)
end