mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
42 lines
1.1 KiB
Ruby
42 lines
1.1 KiB
Ruby
module Fog
|
|
|
|
class SSH
|
|
|
|
def initialize(address, username, options = {})
|
|
unless options[:keys] || options[:password]
|
|
raise ArgumentError.new(':keys or :password are required to initialize SSH')
|
|
end
|
|
@address = address
|
|
@username = username
|
|
@options = options.merge!(:paranoid => false)
|
|
end
|
|
|
|
def run(commands)
|
|
commands = [*commands]
|
|
results = []
|
|
Net::SSH.start(@address, @username, @options) do |ssh|
|
|
commands.each do |command|
|
|
ssh.open_channel do |channel|
|
|
channel.request_pty
|
|
result = { :command => command }
|
|
channel.exec(command.sub(/^sudo/, %q{sudo -p 'fog sudo password:'})) do |channel, success|
|
|
channel.on_data do |channel, data|
|
|
if data.strip == 'fog sudo password:'
|
|
channel.send_data("#{@options[:password]}\n")
|
|
else
|
|
result[:data] ||= ''
|
|
result[:data] << data
|
|
end
|
|
end
|
|
end
|
|
results << result
|
|
end
|
|
ssh.loop
|
|
end
|
|
end
|
|
results
|
|
end
|
|
|
|
end
|
|
|
|
end
|