Class: SimpleSsh::SimpleSSH
- Inherits:
-
Object
- Object
- SimpleSsh::SimpleSSH
- Defined in:
- lib/simple_ssh.rb
Instance Attribute Summary collapse
-
#result_by_host ⇒ Object
readonly
Returns the value of attribute result_by_host.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #cmds ⇒ Object
- #execute ⇒ Object
- #hosts ⇒ Object
-
#initialize(options = {}) ⇒ SimpleSSH
constructor
class methods end.
- #keys ⇒ Object
- #result_by_cmd ⇒ Object
- #to_csv(sep = ",") ⇒ Object
- #to_s ⇒ Object
- #to_yaml(by = :host) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SimpleSSH
class methods end
16 17 18 19 20 21 22 |
# File 'lib/simple_ssh.rb', line 16 def initialize(={}) @hosts = [:hosts] || [] @cmds = [:cmds] || [] @keys = [:keys] || [] @user = [:user] || "" @result_by_host = {} end |
Instance Attribute Details
#result_by_host ⇒ Object (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 |
#user ⇒ Object
Returns the value of attribute user.
24 25 26 |
# File 'lib/simple_ssh.rb', line 24 def user @user end |
Class Method Details
Instance Method Details
#cmds ⇒ Object
31 32 33 |
# File 'lib/simple_ssh.rb', line 31 def cmds @cmds ||= [] end |
#execute ⇒ Object
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 |
#hosts ⇒ Object
27 28 29 |
# File 'lib/simple_ssh.rb', line 27 def hosts @hosts ||= [] end |
#keys ⇒ Object
35 36 37 |
# File 'lib/simple_ssh.rb', line 35 def keys @keys ||= [] end |
#result_by_cmd ⇒ Object
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_s ⇒ Object
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 |