18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/worker.rb', line 18
def go
@util.show_summary(self)
result = ''
@header = @header_begin + @header_padding + @header_end
begin
Net::SSH.start(@hostname, @username, :password => @password, :passphrase => @pkey_password, :non_interactive => true) do |ssh|
channel = ssh.open_channel do |channel, success|
cols = %x{tput cols}.chomp.to_i - @header.length
channel.request_pty(opts={:term=>'xterm',:chars_wide => cols})
@util.dbg("sending command: #{@command}")
channel.exec(@command)
channel.on_data do |channel, data|
attempts = 0
if attempts >= 2
raise 'failed to connect -- too many attempts'
end
if data =~ /Sorry, try again/
raise 'failed to connect -- incorrect sudo password'
end
if data =~ /#{@username}@#{@hostname}'s password:/
raise 'failed to connect -- password failed'
end
if data =~ /^\[sudo\] password for / and attempts == 0
if @sudo_password
channel.send_data "#{@sudo_password}\n"
elsif @password
channel.send_data "#{@password}\n"
else
raise 'failed to connect -- no sudo_password or password defined'
end
attempts += 1
@util.dbg("attempts: #{attempts}")
elsif data =~ /^\[sudo\] password for / and attempts == 1
channel.send_data "#{@password}\n"
attempts += 1
@util.dbg("attempts: #{attempts}")
end
unless @block
@util.display_data(@header, data)
else
result += data.to_s
end
end
end
channel.wait
if @block
@util.display_data(@header, result)
puts "\n"
end
end
rescue SocketError => e
@util.display_error(e)
puts "Failed to connect to #{@hostname}\n".red
rescue RuntimeError => e
@util.display_error(e)
puts "#{@hostname} -- incorrect password, failed to connect".red
rescue => e
@util.display_error(e)
end
end
|