Class: Jssh

Inherits:
Object
  • Object
show all
Defined in:
lib/jssh.rb,
lib/jssh/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJssh

Returns a new instance of Jssh.



8
9
10
11
12
# File 'lib/jssh.rb', line 8

def initialize
	@auth_cfg={}
       self.printer=Printer.new(StdoutExecutor.new)
	@queue=Queue.new
end

Instance Attribute Details

#auth_cfgObject

Returns the value of attribute auth_cfg.



6
7
8
# File 'lib/jssh.rb', line 6

def auth_cfg
  @auth_cfg
end

#cmdObject

Returns the value of attribute cmd.



6
7
8
# File 'lib/jssh.rb', line 6

def cmd
  @cmd
end

#hostsObject

Returns the value of attribute hosts.



6
7
8
# File 'lib/jssh.rb', line 6

def hosts
  @hosts
end

#printerObject

Returns the value of attribute printer.



6
7
8
# File 'lib/jssh.rb', line 6

def printer
  @printer
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/jssh.rb', line 6

def user
  @user
end

Instance Method Details

#execute(from = 0, len = nil, group_size = 1) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jssh.rb', line 38

def execute(from=0,len=nil,group_size=1)
       pr=start_printer(self)
	threads=[]
	len||=self.hosts.size
       to=from+len-1
	divider(self.hosts[from..to].size,group_size) do |df,dt|
		threads<<Thread.new(self,from+df,from+dt) do |rssh,f,t|
			rssh.go(f,t) do |h,r,over|
                   rssh.messages.push({'host'=>h,'content'=>r,'finished'=>over})
               end
		end
	end	
       threads.each{|t| t.join}
       loop{ break if self.printer.finished? }
       Thread.kill pr
end

#go(from = 0, len = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jssh.rb', line 17

def go(from=0,len=nil)
	len||=self.hosts.size
       to=from+len-1
	password=self.auth_cfg[:password]
       keys=[self.auth_cfg[:key]].reject{|x| x.nil?}
	self.hosts[from..to].each_with_index do |host,i|
		begin
		Net::SSH.start(host,self.user,:keys=>keys,:password=>password) do |ssh|
			result=ssh.exec!(self.cmd) || ''
			ssh.exec!(self.cmd) do |ch,stream,data|
                   data||=''
                   yield host,data,false if block_given?
               end
		end
		rescue=>e
               result=e.to_s
		end
           result||=''
           yield host,result,true  if block_given?
	end
end

#messagesObject



13
14
15
# File 'lib/jssh.rb', line 13

def messages
    @queue
end