simple_ssh 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG.rdoc +7 -0
  3. data/README.rdoc +11 -10
  4. data/lib/simple_ssh.rb +45 -33
  5. metadata +47 -49
  6. metadata.gz.sig +0 -0
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,10 @@
1
+ === 0.0.2
2
+
3
+ * 1 major enhancement
4
+
5
+ * feature: Read output of failed commands from STDERR.
6
+
7
+
1
8
  === 0.0.1
2
9
 
3
10
  * 1 major enhancement
@@ -11,7 +11,7 @@ If your answers for these questions are yes then simple_ssh is for you.
11
11
 
12
12
  === Examples
13
13
  ==== Configuration
14
- * Simple configuration of command list execution by hosts using a block.
14
+ * Simple configuration of command list execution by hosts using a block.
15
15
 
16
16
  ssh = SimpleSsh::SimpleSSH.configure do |cfg|
17
17
  cfg.user = "root"
@@ -28,40 +28,40 @@ If your answers for these questions are yes then simple_ssh is for you.
28
28
 
29
29
  * Configuration by dictionary or block or both
30
30
 
31
- ssh = SimpleSsh::SimpleSSH.configure(:cmds => ["ls -l / | wc", "hostname"], :user => "root") do |cfg|
31
+ ssh = SimpleSsh::SimpleSSH.configure(:cmds => ["hostname"], :user => "root") do |cfg|
32
32
  cfg.hosts << "192.168.143.153"
33
33
  cfg.cmds << "whoami"
34
34
  cfg.keys << "~/.ssh/id_rsa"
35
35
  end
36
-
36
+
37
37
  ==== Command execution
38
38
  * Execute the configured hosts and commands
39
39
 
40
40
  ssh.execute
41
41
 
42
42
  * Execute a block
43
-
43
+
44
44
  ssh.execute do |ch|
45
45
  ch.cmds << "echo yeaha"
46
46
  ch.cmds << "echo yeaha | openssl dgst -sha1"
47
47
  end
48
-
48
+
49
49
  ==== Using results
50
50
  * Get results by hosts dictionary
51
51
 
52
- ssh.result_by_host["192.168.143.153"] #=> {"ls -l / | wc"=>" 23 206 1151", "hostname"=>"foreman-squeeze", "whoami"=>"root"}
52
+ ssh.result_by_host["192.168.143.153"] #=> {"hostname"=>"foreman-squeeze", "whoami"=>"root"}
53
53
 
54
54
  * Get results by cmd dictionary
55
55
 
56
56
  ssh.result_by_cmd["whoami"] #=> {"192.168.143.153"=>"root"}
57
57
 
58
- * Dump yaml
58
+ * Dump yaml
59
59
 
60
- ssh.to_yaml #=> "--- \n192.168.143.153: \n ls -l / | wc: \" 23 206 1151\"\n hostname: foreman-squeeze\n whoami: root\n"
60
+ ssh.to_yaml #=> "--- \n192.168.143.153: \n hostname: foreman-squeeze\n whoami: root\n"
61
61
 
62
62
  * Dump csv
63
63
 
64
- ssh.to_csv(";") #=> "host;cmd;result\n192.168.143.153;ls -l / | wc; 23 206 1151\n192.168.143.153;hostname;foreman-squeeze\n192.168.143.153;whoami;root"
64
+ ssh.to_csv(";") #=> "host;cmd;result\n192.168.143.153;hostname;foreman-squeeze\n192.168.143.153;whoami;root"
65
65
 
66
66
  == REQUIREMENTS
67
67
 
@@ -74,7 +74,8 @@ If your answers for these questions are yes then simple_ssh is for you.
74
74
  == AUTHORS
75
75
 
76
76
  Sandor Szücs, sandor.szuecs@fu-berlin.de
77
-
77
+
78
78
  == LICENSE
79
79
 
80
80
  See LICENSE file.
81
+
@@ -1,10 +1,10 @@
1
1
  require "net/ssh"
2
2
 
3
3
  module SimpleSsh
4
- VERSION = '0.0.1'
5
-
4
+ VERSION = '0.0.2'
5
+
6
6
  class SimpleSSH
7
-
7
+
8
8
  class << self
9
9
  def configure(options={})
10
10
  ssh = SimpleSsh::SimpleSSH.new(options)
@@ -12,16 +12,18 @@ module SimpleSsh
12
12
  ssh
13
13
  end
14
14
  end # class methods end
15
-
15
+
16
16
  def initialize(options={})
17
- @hosts = options[:hosts] || []
18
- @cmds = options[:cmds] || []
19
- @keys = options[:keys] || []
20
- @user = options[:user] || ""
17
+ @hosts = options[:hosts] || []
18
+ @cmds = options[:cmds] || []
19
+ @keys = options[:keys] || []
20
+ @user = options[:user] || ""
21
+ @result_by_host = {}
21
22
  end
22
-
23
- attr_accessor :keys, :user
24
-
23
+
24
+ attr_accessor :user
25
+ attr_reader :result_by_host
26
+
25
27
  def hosts
26
28
  @hosts ||= []
27
29
  end
@@ -29,34 +31,33 @@ module SimpleSsh
29
31
  def cmds
30
32
  @cmds ||= []
31
33
  end
32
-
34
+
35
+ def keys
36
+ @keys ||= []
37
+ end
38
+
33
39
  def execute
34
40
  if block_given?
35
41
  @cmds.clear
36
- @result_by_cmd.clear
37
42
  yield(self)
38
43
  end
39
44
 
40
45
  results = {}
41
- @hosts.each do |host|
42
- results[host] = execute_cmds_on(user, host)
46
+ hosts.each do |host|
47
+ @result_by_host[host] = execute_cmds_on(@user, host)
43
48
  end
44
- @result_by_host = results
49
+ result_by_host
45
50
  end
46
-
47
- attr_reader :result_by_host
48
-
51
+
49
52
  def result_by_cmd
50
- return @result_by_cmd if defined? @result_by_cmd and not @result_by_cmd.empty?
51
-
52
- @result_by_cmd = {}
53
- @result_by_host.each do |host, cmd_dict|
53
+ result = {}
54
+ result_by_host.each do |host, cmd_dict|
54
55
  cmd_dict.each do |cmd, res|
55
- @result_by_cmd[cmd] ||= {}
56
- @result_by_cmd[cmd][host]= res
56
+ result[cmd] ||= {}
57
+ result[cmd][host]= res
57
58
  end
58
59
  end
59
- @result_by_cmd
60
+ result
60
61
  end
61
62
 
62
63
  def to_s
@@ -68,13 +69,13 @@ module SimpleSsh
68
69
  res
69
70
  end.join("")
70
71
  end
71
-
72
+
72
73
  def to_yaml(by=:host)
73
74
  require 'yaml' unless defined? YAML
74
75
  s = send "result_by_#{by.to_s}".to_sym
75
76
  YAML.dump(s)
76
77
  end
77
-
78
+
78
79
  def to_csv(sep=",")
79
80
  require "csv" unless defined? CSV
80
81
  csv = []
@@ -90,23 +91,34 @@ module SimpleSsh
90
91
 
91
92
  csv.join("\n")
92
93
  end
93
-
94
+
94
95
  private
95
-
96
+
96
97
  def execute_cmds_on(user, host)
97
98
  result = {}
99
+ except=nil
98
100
  begin
99
- Net::SSH.start( host, user, :keys => keys) do |ssh|
101
+ Net::SSH.start( host, user, :keys => @keys) do |ssh|
100
102
  @cmds.each do |cmd|
101
103
  begin
102
- result[cmd]= ssh.exec!(cmd).chomp
104
+ channel = ssh.open_channel do |channel|
105
+ channel.exec(cmd) do |ch, success|
106
+ ch.on_data do |ch,data|
107
+ result[cmd] = data.chomp
108
+ end
109
+ ch.on_extended_data do |ch, type, data|
110
+ result[cmd] = data.chomp
111
+ end
112
+ end
113
+ end
103
114
  rescue Exception => e
104
115
  $stderr.puts "Failed to execute #{cmd}"
105
116
  end
106
117
  end
107
118
  end
108
119
  rescue Exception => e
109
- $stderr.puts "Could not open connection #{user}@#{host} using private key #{keys}"
120
+ $stderr.puts "Could not open connection #{user}@#{host} using private key #{@keys}"
121
+ raise
110
122
  end
111
123
  result
112
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_ssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,52 +9,39 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain:
12
- - ! '-----BEGIN CERTIFICATE-----
13
-
14
- MIIDQjCCAiqgAwIBAgIBADANBgkqhkiG9w0BAQUFADBHMRYwFAYDVQQDDA1zYW5k
15
-
16
- b3Iuc3p1ZWNzMRkwFwYKCZImiZPyLGQBGRYJZnUtYmVybGluMRIwEAYKCZImiZPy
17
-
18
- LGQBGRYCZGUwHhcNMTEwOTAyMTgyMjM0WhcNMTIwOTAxMTgyMjM0WjBHMRYwFAYD
19
-
20
- VQQDDA1zYW5kb3Iuc3p1ZWNzMRkwFwYKCZImiZPyLGQBGRYJZnUtYmVybGluMRIw
21
-
22
- EAYKCZImiZPyLGQBGRYCZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
23
-
24
- AQCfTZrRtSgalAtriDgJXfjkKscwioBTJiEE6q93AE5/ij9QKPKXIQarZ8ywW6rr
25
-
26
- LNU8i5UgxmAczX0aSvGJcF+Hi4uUW6KrIa51q58eTgkspkNKe7/5PgHcRmKmd5M9
27
-
28
- /ePlN732OogNekY6+mXLcUVpiEVxRpqHScA22pNeO4e7a3o8yHk9ck6PDbH8xYVS
29
-
30
- phIOFhA21c2O2pQ/37umnF6oSC5lKIQqJOZG3Qvi8bcNIzEE5PaN/75lZaSdeTvF
31
-
32
- PztwxxwruXkbWvnx7rVBNNTb7z7RhmWVkuYIhEZLPJR7cLvxqwLJBCBmEVEPUzTb
33
-
34
- zmVnrI3P61Hxy2flPlUEHGVxAgMBAAGjOTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYE
35
-
36
- FMiNx8VcXneWW6aCihufDbjxN1O3MAsGA1UdDwQEAwIEsDANBgkqhkiG9w0BAQUF
37
-
38
- AAOCAQEATfQiXAXiFR6EBQ55WsxLMlf1WITnkB59c8xlwtLRzhmeGQS4kNjaNuMG
39
-
40
- fkkpJzLfpQLmUElsJDxTuTzwvoxP1u0dKCKbdduspJZbhPbJDQfdKa3TkvM+h9kT
41
-
42
- OXBP3qpDrsfvDqfvFNxvKdDWDzYFYFGE90OVdBgFbOzCVbyGeFUk3sz2YKc1TKdz
43
-
44
- 9vfvKROAX09+XFbNgAQl1ZZ8rvH2MEQTjoFX71iyMkgdu7hhFEJ+HR0G58Vp8++O
45
-
46
- WS4A5NGvEAx1OVMHEl5VSh1W94lT3X2h3RclUQZ3rt0WtDHj3a66cqKIDq/5FTlg
47
-
48
- zON3JzXsfb7Swn/Rj4wwFwsjZVTngw==
49
-
50
- -----END CERTIFICATE-----
51
-
52
- '
53
- date: 2011-09-02 00:00:00.000000000Z
12
+ - !binary |-
13
+ LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURRakNDQWlxZ0F3SUJB
14
+ Z0lCQURBT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d1FFQXdJRXNEQU5CZ2txaGtpRzl3MEJBUVVGCkFB
31
+ T0NBUUVBVGZRaVhBWGlGUjZFQlE1NVdzeExNbGYxV0lUbmtCNTljOHhsd3RM
32
+ UnpobWVHUVM0a05qYU51TUcKZmtrcEp6TGZwUUxtVUVsc0pEeFR1VHp3dm94
33
+ UDF1MGRLQ0tiZGR1c3BKWmJoUGJKRFFmZEthM1Rrdk0raDlrVApPWEJQM3Fw
34
+ RHJzZnZEcWZ2Rk54dktkRFdEellGWUZHRTkwT1ZkQmdGYk96Q1ZieUdlRlVr
35
+ M3N6MllLYzFUS2R6Cjl2ZnZLUk9BWDA5K1hGYk5nQVFsMVpaOHJ2SDJNRVFU
36
+ am9GWDcxaXlNa2dkdTdoaEZFSitIUjBHNThWcDgrK08KV1M0QTVOR3ZFQXgx
37
+ T1ZNSEVsNVZTaDFXOTRsVDNYMmgzUmNsVVFaM3J0MFd0REhqM2E2NmNxS0lE
38
+ cS81RlRsZwp6T04zSnpYc2ZiN1N3bi9SajR3d0Z3c2paVlRuZ3c9PQotLS0t
39
+ LUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
40
+ date: 2012-02-22 00:00:00.000000000 Z
54
41
  dependencies:
55
42
  - !ruby/object:Gem::Dependency
56
43
  name: net-ssh
57
- requirement: &2153607580 !ruby/object:Gem::Requirement
44
+ requirement: &2160180880 !ruby/object:Gem::Requirement
58
45
  none: false
59
46
  requirements:
60
47
  - - ~>
@@ -62,18 +49,29 @@ dependencies:
62
49
  version: '2.0'
63
50
  type: :runtime
64
51
  prerelease: false
65
- version_requirements: *2153607580
52
+ version_requirements: *2160180880
53
+ - !ruby/object:Gem::Dependency
54
+ name: rdoc
55
+ requirement: &2160179020 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '3.10'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: *2160179020
66
64
  - !ruby/object:Gem::Dependency
67
65
  name: hoe
68
- requirement: &2153607120 !ruby/object:Gem::Requirement
66
+ requirement: &2160193720 !ruby/object:Gem::Requirement
69
67
  none: false
70
68
  requirements:
71
69
  - - ~>
72
70
  - !ruby/object:Gem::Version
73
- version: '2.12'
71
+ version: '2.13'
74
72
  type: :development
75
73
  prerelease: false
76
- version_requirements: *2153607120
74
+ version_requirements: *2160193720
77
75
  description: ! 'simple_ssh is a simple wrapper around the net-ssh gem.
78
76
 
79
77
 
@@ -118,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
116
  version: '0'
119
117
  requirements: []
120
118
  rubyforge_project: simple_ssh
121
- rubygems_version: 1.8.10
119
+ rubygems_version: 1.8.16
122
120
  signing_key:
123
121
  specification_version: 3
124
122
  summary: simple_ssh is a simple wrapper around the net-ssh gem
metadata.gz.sig CHANGED
Binary file