5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/danarchy_sys/ssh.rb', line 5
def self.new(connector, opts = {})
opts[:timeout] ||= 30
opts[:quiet] ||= false
pid, stdout, stderr = nil
ssh = "ssh -i '#{connector[:ssh_key]}' #{connector[:ssh_user]}@#{connector[:ipv4]} "
ssh += "-o StrictHostKeyChecking=no "
ssh += "-o ConnectTimeout=#{opts[:timeout]} " if opts[:timeout]
if opts[:command]
puts "Running '#{opts[:command]}' on '#{connector[:ipv4]}'" unless opts[:quiet]
ssh += Shellwords.shellescape(opts[:command])
Open3.popen3(ssh) do |i, o, e, t|
pid = t.pid
(out, err) = o.read, e.read
stdout = !out.empty? ? out : nil
stderr = !err.empty? ? err : nil
end
else
return system(ssh)
end
if opts[:quiet] == false
puts "------\nErrored at: #{caller_locations.first.label} Line: #{caller_locations.first.lineno}\nSTDERR: ", stderr, '------' if stderr
puts "------\nSTDOUT: ", stdout, '------' if stdout
end
{ pid: pid, stdout: stdout, stderr: stderr }
end
|