simple_ssh 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,6 @@
1
+ === 0.0.1
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+ * initial commit
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sandor Szücs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ CHANGELOG.rdoc
2
+ LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/simple_ssh.rb
@@ -0,0 +1,80 @@
1
+ = SimpleSSH
2
+ home :: https://github.com/szuecs/simple_ssh
3
+
4
+ == DESCRIPTION
5
+ simple_ssh is a simple wrapper around the net-ssh gem.
6
+
7
+ * You want to execute a list of commands on a list of hosts using ssh?
8
+ * You want to analyze the results within a script?
9
+
10
+ If your answers for these questions are yes then simple_ssh is for you.
11
+
12
+ === Examples
13
+ ==== Configuration
14
+ * Simple configuration of command list execution by hosts using a block.
15
+
16
+ ssh = SimpleSsh::SimpleSSH.configure do |cfg|
17
+ cfg.user = "root"
18
+ cfg.hosts << "192.168.143.153"
19
+ cfg.cmds << "whoami"
20
+ cfg.cmds << "id"
21
+ cfg.keys << "~/.ssh/id_rsa"
22
+ end
23
+ ssh.execute #=> {"192.168.143.153"=>{"whoami"=>"root", "id"=>"uid=0(root) gid=0(root) groups=0(root)"}}
24
+ puts ssh
25
+ #192.168.143.153:
26
+ # whoami: root
27
+ # id: uid=0(root) gid=0(root) groups=0(root)
28
+
29
+ * Configuration by dictionary or block or both
30
+
31
+ ssh = SimpleSsh::SimpleSSH.configure(:cmds => ["ls -l / | wc", "hostname"], :user => "root") do |cfg|
32
+ cfg.hosts << "192.168.143.153"
33
+ cfg.cmds << "whoami"
34
+ cfg.keys << "~/.ssh/id_rsa"
35
+ end
36
+
37
+ ==== Command execution
38
+ * Execute the configured hosts and commands
39
+
40
+ ssh.execute
41
+
42
+ * Execute a block
43
+
44
+ ssh.execute do |ch|
45
+ ch.cmds << "echo yeaha"
46
+ ch.cmds << "echo yeaha | openssl dgst -sha1"
47
+ end
48
+
49
+ ==== Using results
50
+ * Get results by hosts dictionary
51
+
52
+ ssh.result_by_host["192.168.143.153"] #=> {"ls -l / | wc"=>" 23 206 1151", "hostname"=>"foreman-squeeze", "whoami"=>"root"}
53
+
54
+ * Get results by cmd dictionary
55
+
56
+ ssh.result_by_cmd["whoami"] #=> {"192.168.143.153"=>"root"}
57
+
58
+ * Dump yaml
59
+
60
+ ssh.to_yaml #=> "--- \n192.168.143.153: \n ls -l / | wc: \" 23 206 1151\"\n hostname: foreman-squeeze\n whoami: root\n"
61
+
62
+ * Dump csv
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"
65
+
66
+ == REQUIREMENTS
67
+
68
+ * net-ssh
69
+
70
+ == INSTALL
71
+
72
+ % sudo gem install simple_ssh
73
+
74
+ == AUTHORS
75
+
76
+ Sandor Szücs, sandor.szuecs@fu-berlin.de
77
+
78
+ == LICENSE
79
+
80
+ See LICENSE file.
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'hoe'
4
+
5
+ Hoe::add_include_dirs("lib")
6
+
7
+ Hoe.plugins.delete :rubyforge
8
+ Hoe.plugin :git
9
+
10
+ Hoe.spec 'simple_ssh' do
11
+ developer "Sandor Szücs", 'sandor.szuecs@fu-berlin.de'
12
+
13
+ self.flay_threshold = 100
14
+
15
+ self.history_file = "CHANGELOG.rdoc"
16
+ self.readme_file = "README.rdoc"
17
+
18
+ extra_deps << ['net-ssh', '~> 2.0']
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,114 @@
1
+ require "net/ssh"
2
+
3
+ module SimpleSsh
4
+ VERSION = '0.0.1'
5
+
6
+ class SimpleSSH
7
+
8
+ class << self
9
+ def configure(options={})
10
+ ssh = SimpleSsh::SimpleSSH.new(options)
11
+ yield(ssh) if block_given?
12
+ ssh
13
+ end
14
+ end # class methods end
15
+
16
+ def initialize(options={})
17
+ @hosts = options[:hosts] || []
18
+ @cmds = options[:cmds] || []
19
+ @keys = options[:keys] || []
20
+ @user = options[:user] || ""
21
+ end
22
+
23
+ attr_accessor :keys, :user
24
+
25
+ def hosts
26
+ @hosts ||= []
27
+ end
28
+
29
+ def cmds
30
+ @cmds ||= []
31
+ end
32
+
33
+ def execute
34
+ if block_given?
35
+ @cmds.clear
36
+ @result_by_cmd.clear
37
+ yield(self)
38
+ end
39
+
40
+ results = {}
41
+ @hosts.each do |host|
42
+ results[host] = execute_cmds_on(user, host)
43
+ end
44
+ @result_by_host = results
45
+ end
46
+
47
+ attr_reader :result_by_host
48
+
49
+ 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|
54
+ cmd_dict.each do |cmd, res|
55
+ @result_by_cmd[cmd] ||= {}
56
+ @result_by_cmd[cmd][host]= res
57
+ end
58
+ end
59
+ @result_by_cmd
60
+ end
61
+
62
+ def to_s
63
+ result_by_host.map do |host, cmd_dict|
64
+ res = "#{host}:\n"
65
+ cmd_dict.each do |cmd,cmd_res|
66
+ res << "\t#{cmd}: #{cmd_res.split("\n").join("\n\t\t")}\n"
67
+ end
68
+ res
69
+ end.join("")
70
+ end
71
+
72
+ def to_yaml(by=:host)
73
+ require 'yaml' unless defined? YAML
74
+ s = send "result_by_#{by.to_s}".to_sym
75
+ YAML.dump(s)
76
+ end
77
+
78
+ def to_csv(sep=",")
79
+ require "csv" unless defined? CSV
80
+ csv = []
81
+
82
+ cols = ["host", "cmd", "result"]
83
+ csv << cols.join(sep)
84
+
85
+ result_by_host.each do |host, cmd_dict|
86
+ cmd_dict.each do |cmd, res|
87
+ csv << (host + sep + cmd + sep + cmd_dict[cmd])
88
+ end
89
+ end
90
+
91
+ csv.join("\n")
92
+ end
93
+
94
+ private
95
+
96
+ def execute_cmds_on(user, host)
97
+ result = {}
98
+ begin
99
+ Net::SSH.start( host, user, :keys => keys) do |ssh|
100
+ @cmds.each do |cmd|
101
+ begin
102
+ result[cmd]= ssh.exec!(cmd).chomp
103
+ rescue Exception => e
104
+ $stderr.puts "Failed to execute #{cmd}"
105
+ end
106
+ end
107
+ end
108
+ rescue Exception => e
109
+ $stderr.puts "Could not open connection #{user}@#{host} using private key #{keys}"
110
+ end
111
+ result
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_ssh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sandor Szücs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain:
12
+ - ! '-----BEGIN CERTIFICATE-----
13
+
14
+ MIIDQjCCAiq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sz2YKc1TKdz
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
54
+ dependencies:
55
+ - !ruby/object:Gem::Dependency
56
+ name: net-ssh
57
+ requirement: &2153607580 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: *2153607580
66
+ - !ruby/object:Gem::Dependency
67
+ name: hoe
68
+ requirement: &2153607120 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ version: '2.12'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: *2153607120
77
+ description: ! 'simple_ssh is a simple wrapper around the net-ssh gem.
78
+
79
+
80
+ * You want to execute a list of commands on a list of hosts using ssh?
81
+
82
+ * You want to analyze the results within a script?
83
+
84
+
85
+ If your answers for these questions are yes then simple_ssh is for you.'
86
+ email:
87
+ - sandor.szuecs@fu-berlin.de
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files:
91
+ - Manifest.txt
92
+ files:
93
+ - CHANGELOG.rdoc
94
+ - LICENSE
95
+ - Manifest.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - lib/simple_ssh.rb
99
+ homepage: https://github.com/szuecs/simple_ssh
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options:
103
+ - --main
104
+ - README.rdoc
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project: simple_ssh
121
+ rubygems_version: 1.8.10
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: simple_ssh is a simple wrapper around the net-ssh gem
125
+ test_files: []
Binary file