tinyssh 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ tinyssh is a little ruby script to batch execute command, upload/download file or directory with ssh
2
+
3
+ ## Require
4
+ you should have ruby installed and some support packages for ssh
5
+
6
+ - ruby
7
+ - rubygems
8
+ - net-ssh
9
+ - net-scp
10
+ - highline
11
+
12
+ you can install them by follow command if you use RPM base Linux
13
+
14
+ ```
15
+ yum -y install ruby rubygems
16
+ gem install net-ssh
17
+ gem install net-scp
18
+ gem install highline
19
+ gem install tinyssh
20
+
21
+ find / -name tinyssh # find where tinyssh installed
22
+ ln -sv location_of_tinyssh /usr/local/bin # if you use rvm to manage multi version ruby, you don't need do this
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```
28
+ Usage: ruby tinyssh.rb run|upload|download <position parameters> [option]
29
+
30
+ Specific options:
31
+ -u, --user [user] Specify a user for accessing remote hosts.(default: root)
32
+ -p, --port [port] Port to connect to on the remote host(default: 22)
33
+ -t, --timeout [time] Set the command timeout to seconds(default: 0)
34
+ -l, --list-file [file] File containing a list of hosts' IP Address
35
+ -o, --output-file [file] Output result to log file
36
+ -i, --identity-key [key] identity (private) key for authentication(default: /root/.ssh/id_rsa)
37
+ -n, --number-of-thread [number] Specify the number of concurrent jobs for each execution
38
+ -h, --help Show this message
39
+ ```
40
+
41
+ - Run command
42
+
43
+ ```
44
+ ruby tinyssh.rb run "command" -l my_host.txt -u root -p 22 -t 10 -i my_keys -n 10
45
+ ```
46
+
47
+ - Upload file or directory
48
+
49
+ ```
50
+ ruby tinyssh.rb upload src_file_or_directory dst_directory -l my_host.txt -u root -p 22 -t 10 -i my_keys -n 10
51
+ ```
52
+
53
+ - Download file or directory
54
+
55
+ ```
56
+ ruby tinyssh.rb download src_file_or_directory dst_directory -l my_host.txt -u root -p 22 -t 10 -i my_keys -n 10
57
+ ```
58
+
59
+ Note: `run`, `upload`, `download`, can be `r`, `u`, `d`
60
+ Note: if you use key to authorize and your key is `/root/.ssh/id_rsa`, you don't need to specify `-i /root/.ssh/id_rsa`, but it will ask for password, just press enter
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/bin/tinyssh CHANGED
@@ -14,24 +14,28 @@ require 'tinyssh'
14
14
  T = []
15
15
  HOST = []
16
16
  OUTPUT = {}
17
- OUTPUT_TITLE = "-------------------------- EXECUTE STATUS --------------------------"
18
17
 
19
18
  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 }
19
+ if ['run', 'r', 'u', 'up', 'upload', 'd', 'dl', 'download'].include?(ARGV[0]) and options.host_file != nil
20
+ File.open(options.host_file).each_line do |line|
21
+ HOST << line.gsub("\n", '')
22
+ end
22
23
 
23
- if options.host_file != nil
24
- File.open(options.host_file).each_line do |line|
25
- HOST << line.gsub("\n", '')
26
- end
24
+ if options.keys.length == 1
25
+ password = ask("Enter password: ") { |q| q.echo = false }
26
+ else
27
+ password = ''
28
+ end
29
+
30
+ if password == ''
31
+ passphrase = ask("Enter passphrash: ") { |q| q.echo = false }
27
32
  else
28
- TinysshOptparse.parse(['-h'])
29
- exit
33
+ passphrase = ''
30
34
  end
31
35
 
32
36
  HOST.each_with_index do |host, index|
33
37
  T << Thread.new {
34
- session = Net::SSH.conn(host, options.user, :port => options.port, :keys => options.keys, :timeout => options.timeout, :passphrase => passphrase.chomp)
38
+ session = Net::SSH.conn(host, options.user, :port => options.port, :password => password, :keys => options.keys, :timeout => options.timeout, :passphrase => passphrase.chomp)
35
39
 
36
40
  if session.class == String
37
41
  OUTPUT[index] = session % host
data/lib/tinyssh.rb CHANGED
@@ -8,6 +8,7 @@ include Net::SSH
8
8
  module Net::SSH
9
9
  def conn(*options)
10
10
  begin
11
+ puts 'try to connction'
11
12
  return Net::SSH.start(*options)
12
13
  rescue Timeout::Error
13
14
  return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connecting Timeout".red + " ] !\n"
@@ -17,7 +18,7 @@ module Net::SSH
17
18
  return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connection Refused".red + " ] !\n"
18
19
  rescue Net::SSH::AuthenticationFailed
19
20
  return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Authentication Failure".red + " ] !\n"
20
- else
21
+ rescue
21
22
  return "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Name or Server not know".red + " ] !\n"
22
23
  end
23
24
  end
@@ -36,7 +36,6 @@ class TinysshOptparse
36
36
  opts.on("-l", "--list-file [file]",
37
37
  "File containing a list of hosts' IP Address") do |file|
38
38
  options.host_file = file
39
- return true
40
39
  end
41
40
 
42
41
  opts.on("-o", "--output-file [file]",
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+
3
+ require 'colorize'
4
+ require 'tinysshoptparse'
5
+ require 'tinyssh'
6
+
7
+ OUTPUT_TITLE = "-------------------------- EXECUTE STATUS --------------------------"
8
+
9
+ class TinysshTest < Test::Unit::TestCase
10
+ def test_colorize
11
+ assert_equal "\e[31mred\e[0m", "red".red
12
+ assert_equal "\e[32mgreen\e[0m", "green".green
13
+ end
14
+
15
+ def test_tinysshoptparse
16
+ assert_equal 'test.txt', TinysshOptparse.parse(['-l', 'test.txt']).host_file
17
+ end
18
+
19
+ def test_tinyssh
20
+ assert_equal "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connection Refused".red + " ] !\n",
21
+ Net::SSH.conn('127.0.0.1', 'root', :port => '222', :timeout => 2)
22
+ assert_equal "\n#{OUTPUT_TITLE}\nHost : %s [ " + "Connecting Timeout".red + " ] !\n",
23
+ Net::SSH.conn('192.168.8.9', 'root', :port => '22', :timeout => 2)
24
+ end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinyssh
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - William Herry
@@ -30,6 +30,9 @@ files:
30
30
  - lib/colorize.rb
31
31
  - lib/tinysshoptparse.rb
32
32
  - lib/tinyssh.rb
33
+ - README.md
34
+ - Rakefile
35
+ - test/test_tinyssh.rb
33
36
  - bin/tinyssh
34
37
  homepage: https://rubygems.org/gems/tinyssh
35
38
  licenses: []