tinyssh 0.0.1

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.
data/bin/tinyssh ADDED
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'rubygems'
6
+ require 'highline/import'
7
+ require 'net/ssh'
8
+ require 'net/scp'
9
+
10
+ require 'lib/colorize'
11
+ require 'lib/tinysshoptparse'
12
+ require 'lib/tinyssh'
13
+
14
+ T = []
15
+ HOST = []
16
+ OUTPUT = {}
17
+ OUTPUT_TITLE = "-------------------------- EXECUTE STATUS --------------------------"
18
+
19
+ def main(options)
20
+ if ['run', 'r', 'u', 'up', 'upload', 'd', 'dl', 'download'].include?(ARGV[0])
21
+ passphrase = ask("Enter passphrash: ") { |q| q.echo = false }
22
+
23
+ if options.host_file != nil
24
+ File.open(options.host_file).each_line do |line|
25
+ HOST << line.gsub("\n", '')
26
+ end
27
+ else
28
+ TinysshOptparse.parse(['-h'])
29
+ puts 'ss'
30
+ exit
31
+ end
32
+
33
+ HOST.each_with_index do |host, index|
34
+ T << Thread.new {
35
+ session = Net::SSH.conn(host, options.user, :port => options.port, :keys => options.keys, :timeout => options.timeout, :passphrase => passphrase.chomp)
36
+
37
+ if session.class == String
38
+ OUTPUT[index] = session % host
39
+ next
40
+ end
41
+
42
+ case ARGV[0]
43
+ when 'r', 'run' then OUTPUT[index] = session.run(ARGV[1])
44
+ when 'u', 'up', 'upload' then OUTPUT[index] = session.up(ARGV[1], ARGV[2])
45
+ when 'd', 'dl', 'download' then OUTPUT[index] = session.dl(ARGV[1], ARGV[2])
46
+ end
47
+ }
48
+ end
49
+
50
+ T.each do |t|
51
+ t.join
52
+ end
53
+
54
+ OUTPUT.keys.sort.each do |k|
55
+ print OUTPUT[k]
56
+ end
57
+
58
+ if options.output_file != nil
59
+ File.open(options.output_file, 'w') do |f|
60
+ OUTPUT.keys.sort.each do |k|
61
+ f.write(OUTPUT[k])
62
+ end
63
+ end
64
+ end
65
+
66
+ else
67
+ TinysshOptparse.parse(['-h'])
68
+ exit
69
+ end
70
+ end
71
+
72
+ main(TinysshOptparse.parse(ARGV))
data/lib/colorize.rb ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class String
4
+ # colorization
5
+ def colorize(text, color_code)
6
+ "\e[#{color_code}m#{text}\e[0m"
7
+ end
8
+
9
+ def red
10
+ colorize(self, 31)
11
+ end
12
+
13
+ def green
14
+ colorize(self, 32)
15
+ end
16
+ end
data/lib/tinyssh.rb ADDED
@@ -0,0 +1,41 @@
1
+ include Net::SSH
2
+
3
+ module Net::SSH
4
+ def conn(*options)
5
+ begin
6
+ return Net::SSH.start(*options)
7
+ rescue Timeout::Error
8
+ return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connecting Timeout".red + " ] !\n"
9
+ rescue Errno::EHOSTUNREACH
10
+ return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Unreachable".red + " ] !\n"
11
+ rescue Errno::ECONNREFUSED
12
+ return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connection Refused".red + " ] !\n"
13
+ rescue Net::SSH::AuthenticationFailed
14
+ return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Authentication Failure".red + " ] !\n"
15
+ end
16
+ end
17
+ end
18
+
19
+ class Net::SSH::Connection::Session
20
+ def run(cmd)
21
+ output = self.exec!(cmd)
22
+ return "\n#{output}#{OUTPUT_TITLE}\nExecute \"#{cmd}\" at #{host}: [ ", "OK".green, " ]\n"
23
+ rescue
24
+ return "\n#{OUTPUT_TITLE}\nExecute \"#{cmd}\" at #{host}: [ ", "FAIL".red, " ] !\n"
25
+ end
26
+
27
+ def up(src, dst)
28
+ self.scp.upload!(src, dst, :recursive => true)
29
+ return "\n#{OUTPUT_TITLE}\nUpload #{host}:#{src} to #{dst}: [ ", "OK".green, " ] !\n"
30
+ rescue
31
+ return "\n#{OUTPUT_TITLE}\nUpload #{host}:#{src} to #{dst}: [ ", "FAIL".red, " ] !\n"
32
+ end
33
+
34
+ def dl(src, dst)
35
+ self.scp.download!(src, dst, :recursive => true)
36
+ return "\n#{OUTPUT_TITLE}\nDownload #{host}:#{src} to #{dst}: [ ", "OK".green, " ] !\n"
37
+ rescue
38
+ return "\n#{OUTPUT_TITLE}\nDownload #{host}:#{src} to #{dst}: [ ", "FAIL".red, " ] !\n"
39
+ end
40
+ end
41
+
@@ -0,0 +1,62 @@
1
+ class TinysshOptparse
2
+ def self.parse(args)
3
+ options = OpenStruct.new
4
+ options.user = 'root'
5
+ options.port = '59878'
6
+ options.num = '0'
7
+ options.keys = ['/root/.ssh/id_rsa']
8
+ options.timeout = 5
9
+ options.host_file = nil
10
+ options.output_file = nil
11
+
12
+ opts = OptionParser.new do |opts|
13
+ opts.banner = "\nUsage: tinyssh.rb run|upload|download <position parameters> [option]"
14
+
15
+ opts.separator ""
16
+ opts.separator "Specific options:"
17
+
18
+ opts.on("-u", "--user [user]",
19
+ "Specify a user for accessing remote hosts.(default: root)") do |user|
20
+ options.user = user
21
+ end
22
+
23
+ opts.on("-p", "--port [port]",
24
+ "Port to connect to on the remote host(default: 59878)") do |port|
25
+ options.port = port
26
+ end
27
+
28
+ opts.on("-t", "--timeout [time]",
29
+ "Set the command timeout to seconds(default: 0)") do |timeout|
30
+ options.timeout = timeout.to_i
31
+ end
32
+
33
+ opts.on("-l", "--list-file [file]",
34
+ "File containing a list of hosts' IP Address") do |file|
35
+ options.host_file = file
36
+ end
37
+
38
+ opts.on("-o", "--output-file [file]",
39
+ "Output result to log file") do |output_file|
40
+ options.output_file = output_file
41
+ end
42
+
43
+ opts.on("-i", "--identity-keys [keys]",
44
+ "identity (private) keys for authentication(default: /root/.ssh/id_rsa)") do |keys|
45
+ options.keys = keys
46
+ end
47
+
48
+ opts.on("-n", "--number-of-thread [number]",
49
+ "Specify the number of concurrent jobs for each execution") do |num|
50
+ options.num = num
51
+ end
52
+
53
+ opts.on_tail("-h", "--help", "Show this message") do
54
+ puts opts
55
+ exit
56
+ end
57
+ end
58
+
59
+ opts.parse!(args)
60
+ options
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinyssh
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - William Herry
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-02 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: run ssh command, upload/download file/directory on/to muti host
22
+ email: WilliamHerryChina@Gmail.com
23
+ executables:
24
+ - tinyssh
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/colorize.rb
31
+ - lib/tinysshoptparse.rb
32
+ - lib/tinyssh.rb
33
+ - bin/tinyssh
34
+ homepage: https://rubygems.org/gems/tinyssh
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Tiny SSH
67
+ test_files: []
68
+