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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/Chee.rb', line 48
def ssh raw_cmd
command = raw_cmd.strip
if command["\n"]
return command.split("\n").map(&:strip).map { |s|
ssh s
}
end
stdout = ""
stderr = ""
t = nil
ip, user, new_opts = *server
opts = {:timeout=>3}.merge(new_opts || {})
begin
get_input = true
@channel = nil
cmd = ''
prev_cmd = ''
prev_data = ''
result = Result.new
t = Thread.new {
while get_input do
cmd = begin
Readline.readline("", true).strip
rescue Interrupt => e get_input = false
"^C"
end
if @channel
@channel.process
else
print "Connection closed. Could not send: #{cmd}\n"
end
end
}
Net::SSH.start(ip, user, opts) { |ssh|
@channel = ssh.open_channel do |ch1|
ch1.on_extended_data do |ch, type, d|
stderr << d
end
ch1.on_request 'exit-status' do |ch, d|
result.exit_status d.read_long
end
ch1.on_open_failed { |ch, code, desc|
stderr << "Failure to open channel: #{code.inspect}: #{desc}"
}
ch1.on_process do |ch|
if cmd.strip == '^C'
ch.send_data( Net::SSH::Buffer.from(:byte, 3, :raw, "\n").to_s )
stderr << "User requested interrupt."
else
if cmd.strip.empty?
else
ch.send_data( "#{cmd}\n" )
prev_cmd = cmd
cmd = ''
end
end
end
ch1.on_data do |ch, d|
stdout << d
unless prev_cmd.to_s.strip == d.strip
print_data d
end
prev_data = d
end
ch1.request_pty do |ch, success|
if success
else
ch.close
(stderr << "Unknown error requesting pty.")
end
end
ch1.exec(command)
end
ssh.loop 0.1
}
rescue Timeout::Error => e
raise e.class, server.inspect
rescue Net::SSH::AuthenticationFailed => e
raise e.class, "Using: #{server.inspect}"
ensure
get_input = false
t.exit if t
end
result.err stderr
result.out stdout
if !result.err.empty? || result.exit_status != 0
e = Exit_Error.new("Exit: #{result.exit_status}, COMMAND: #{command}")
e.exit_status result.exit_status
e.out result.out
e.err result.err
raise e
end
result.out.strip!
result
end
|