jssh 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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +55 -2
  3. data/bin/jssh +39 -20
  4. data/lib/jssh/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c537fc623582c56e6324e18cb8d6c18e496109b4
4
- data.tar.gz: 3db80a7f5038a1a84de106ad96ef65e80027c5bc
3
+ metadata.gz: 802cfe834425216c50891414dec0f5a71b4c6498
4
+ data.tar.gz: bca4531307cf24e4c6b654cd7c0a791b0acf8672
5
5
  SHA512:
6
- metadata.gz: f9a9c26b683fa6f4fa4f1e98531f4bc0934497c7d8ed2cba3db7e146d512204cbfb16c27de9255b54da51a3cee2a6a8b550208989fd79f1cf1500fa20f7ff74f
7
- data.tar.gz: 05e5cbd8b1e2ba7c541aae76e28aa4d7f58e4d8adec55069c4228d6024844520dbcc44589de2ae74f3e234d20eb1a0da522d95c7d1e82252ccf205bc8b8ca40c
6
+ metadata.gz: 4e7e49858f77e2d1d164d257a1197be768bbab91090a4650dbe811663c9b9d8b0bb944f6f16b268654b77f913bd399dd19b6bfc27b5b56a9d472fd226a82413c
7
+ data.tar.gz: dadbd6e0b570a80dd86568bbd5faccd83a30258a57a086333e5cb57358de33864b4938a418cbd8f8aee22c9df1e4503991bc8306529706ae44328527db03d648
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Jssh
2
2
 
3
- TODO: Write a gem description
3
+ Jssh is a batch ssh tool, which OP would like.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,60 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Simple Usage:
22
+
23
+ $ jssh -f hosts -u jason -p jason 'hostname'
24
+ ====================172.16.39.139====================
25
+ localcentos.vm
26
+ ====================192.168.42.136====================
27
+ localcentos.vm
28
+ Finished!
29
+
30
+ The hosts is a hosts file which lists every host per line like:
31
+
32
+ 172.16.39.139
33
+
34
+ 192.168.42.136
35
+
36
+ If you have configured the trusted relationship with each host, you can simply use:
37
+
38
+ $ jssh -f hosts -u jason 'hostname'
39
+
40
+ Go further, if you place a file `~/.jsshrc`,and the content is:
41
+
42
+ keyfile: /home/USER/.ssh/id_rsa
43
+ user: jason
44
+
45
+ Then you can get more easier:
46
+
47
+ $ jssh -f hosts 'hostname'
48
+
49
+ And, if you place multiple commands in a script such as `/path/to/script`, you can use like:
50
+
51
+ $ jssh -f hosts -c /path/to/script
52
+
53
+ At last, I think this is very charming for OP, we can use a batch tool with pause. Just use the `-b or --break` option:
54
+
55
+ $ jssh -f hosts 'hostname' -b
56
+
57
+ After the operation of the first host is done, the jssh can auto login to the first host, which lets us can check our operation is ok.
58
+
59
+ ====================172.16.39.139====================
60
+ localcentos.vm
61
+ Now the operations on the first host is completed, we're going to it for a check.
62
+ Press ENTER to automatic to login 172.16.39.139 ...
63
+
64
+ Last login: Sat Mar 29 00:57:07 2014 from 172.16.39.1
65
+ jason@localcentos:~
66
+
67
+ After you finish check, `exit` back to jssh, we can choose to parallel or serial to finish the left operations, or just `Ctrl+C` to exit.
68
+
69
+ Continue to finish all the left operations by parallel(p) or serial(s), [p/s] p
70
+ ====================192.168.42.136====================
71
+ localcentos.vm
72
+ Finished!
73
+
74
+ At last, type `jssh --help` for more help.
22
75
 
23
76
  ## Contributing
24
77
 
data/bin/jssh CHANGED
@@ -42,7 +42,7 @@ OptionParser.new do |opts|
42
42
  opts.on("-f file","--hostfile file", "host file, every host occupy a line") do |v|
43
43
  options[:hostfile] = v
44
44
  unless File.exists? v
45
- puts "host file doesn't exists!".to_red
45
+ puts "host file doesn't exists!".to_red
46
46
  exit
47
47
  end
48
48
  end
@@ -58,16 +58,13 @@ OptionParser.new do |opts|
58
58
  opts.on("-i keyfile", "the ssh id_rsa file") do |v|
59
59
  options[:key] = v
60
60
  end
61
- opts.on("--nopause", "no pause for first operation") do |v|
62
- options[:nopause] = v
61
+ opts.on("-b","--break", "pause after first operation") do |v|
62
+ options[:pause] = v
63
63
  end
64
- opts.on("-c commands","--command commands", "commands") do |v|
65
- options[:command] = v
66
- end
67
- opts.on("-C cmdfile","--cmdfile cmdfile", "script path") do |v|
64
+ opts.on("-c cmdfile","--cmdfile cmdfile", "script path") do |v|
68
65
  options[:cmdfile] = v
69
66
  unless File.exists? v
70
- puts "command file doesn't exists!".to_red
67
+ puts "command file doesn't exists!".to_red
71
68
  exit
72
69
  end
73
70
  end
@@ -75,7 +72,12 @@ OptionParser.new do |opts|
75
72
  puts opts
76
73
  exit
77
74
  end
75
+ opts.on("--debug", "debug inputs") do |v|
76
+ options[:debug]=v
77
+ end
78
78
  end.parse!
79
+
80
+ # If neither the rsa file or password specified, go get rsa file in home directory
79
81
  unless options[:key] || options[:password]
80
82
  if File.exists? ENV['HOME']+"/.jsshrc"
81
83
  config=YAML::load_file(ENV['HOME']+"/.jsshrc")
@@ -85,6 +87,19 @@ unless options[:key] || options[:password]
85
87
  options[:key]||=ENV['HOME']+'/.ssh/id_rsa'
86
88
  end
87
89
  end
90
+
91
+ # If no script file, read command from command line
92
+ options[:command]=ARGV.first if ARGV.size>0 && options[:cmdfile].nil?
93
+
94
+ # If no user specified, use current user
95
+ options[:user]||=ENV["USER"]
96
+
97
+ if options[:debug]
98
+ puts "options: ".to_green+options.to_s
99
+ puts "ARGV: ".to_green+ARGV.to_s
100
+ exit
101
+ end
102
+
88
103
  lambda{puts "No command or command file specify";exit}.call unless options[:cmdfile] || options[:command]
89
104
  lambda{puts "Please specify the user";exit}.call unless options[:user]
90
105
  lambda{puts "Please specify the rsa file or password";exit}.call unless options[:key] || options[:password]
@@ -96,15 +111,11 @@ class Jssh
96
111
  @auth_cfg={}
97
112
  self.printer=:on
98
113
  @queue=Queue.new
99
- Thread.new(self) do |rssh|
100
- while true
101
- puts rssh.messages.pop if rssh.printer
102
- end
103
- end
104
114
  end
105
115
  def messages
106
116
  @queue
107
117
  end
118
+
108
119
  def go(from=0,len=nil)
109
120
  len||=self.hosts.size
110
121
  to=from+len-1
@@ -123,23 +134,31 @@ class Jssh
123
134
  end
124
135
  end
125
136
  def execute(from=0,len=nil,group_size=10)
137
+ pr=start_printer(self)
126
138
  threads=[]
127
139
  len||=self.hosts.size
128
140
  to=from+len-1
129
141
  divider(self.hosts[from..to].size,group_size) do |df,dt|
130
142
  threads<<Thread.new(self,from+df,from+dt) do |rssh,f,t|
131
143
  rssh.go(f,t) do |h,r|
132
- output="="*20+h+"="*20+"\n"+r
144
+ output=("="*20+h+"="*20).to_yellow+"\n"+r
133
145
  rssh.messages.push output
134
146
  end
135
147
  end
136
148
  end
137
- while threads.find{|t| t.alive? }
138
- sleep 1
139
- end
149
+ threads.each{|t| t.join}
150
+ loop{ break if self.messages.size==0 } if self.printer
151
+ Thread.kill pr
140
152
  end
141
153
 
142
154
  private
155
+ def start_printer(jssh)
156
+ Thread.new(jssh) do |rssh|
157
+ while true
158
+ puts rssh.messages.pop if rssh.printer
159
+ end
160
+ end
161
+ end
143
162
  def divider(total,gsize)
144
163
  gcnt=total/gsize
145
164
  gcnt=gcnt+1 if total%gsize!=0
@@ -165,7 +184,7 @@ if options[:cmdfile]
165
184
  else
166
185
  rs.cmd=options[:command]
167
186
  end
168
- if options[:nopause]
187
+ unless options[:pause]
169
188
  rs.hosts=orig_hosts
170
189
  rs.execute 0,nil,rs.hosts.size
171
190
  puts "Finished!".to_green
@@ -175,7 +194,7 @@ rs.hosts=orig_hosts.take 1
175
194
  rs.execute
176
195
  puts "Now the operations on the first host is completed, we're going to it for a check.".to_yellow
177
196
  print "Press ENTER to automatic to login #{rs.hosts.first.to_red} ..."
178
- gets
197
+ $stdin.gets
179
198
  if options[:password]
180
199
  login_to_first=%Q|expect -c 'spawn ssh #{rs.user}@#{rs.hosts.first}
181
200
  expect {
@@ -190,7 +209,7 @@ else
190
209
  end
191
210
  system(login_to_first)
192
211
  print "Continue to finish all the left operations by parallel(p) or serial(s), [p/s] ".to_yellow
193
- ans=gets.chomp
212
+ ans=$stdin.gets.chomp
194
213
  rs.hosts=orig_hosts[1..-1]
195
214
  if ans.downcase=='p'
196
215
  rs.execute
data/lib/jssh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jssh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - qjpcpu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-28 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler