Class: Fog::SSH::Real
- Inherits:
-
Object
- Object
- Fog::SSH::Real
- Defined in:
- lib/fog/ssh.rb
Instance Method Summary collapse
-
#initialize(address, username, options) ⇒ Real
constructor
A new instance of Real.
- #run(commands) ⇒ Object
Constructor Details
#initialize(address, username, options) ⇒ Real
Returns a new instance of Real.
41 42 43 44 45 |
# File 'lib/fog/ssh.rb', line 41 def initialize(address, username, ) @address = address @username = username @options = .merge!(:paranoid => false) end |
Instance Method Details
#run(commands) ⇒ Object
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 |
# File 'lib/fog/ssh.rb', line 47 def run(commands) commands = [*commands] results = [] begin Net::SSH.start(@address, @username, @options) do |ssh| commands.each do |command| ssh.open_channel do |channel| sudoable_command = command.sub(/^sudo/, %{sudo -p 'fog sudo password:'}) escaped_command = sudoable_command.sub(/'/, %{'"'"'}) channel.request_pty result = Result.new(escaped_command) channel.exec(%{bash -lc '#{escaped_command}'}) do |channel, success| unless success raise "Could not execute command: #{command.inspect}" end channel.on_data do |channel, data| result.stdout << data end channel.on_extended_data do |channel, type, data| next unless type == 1 result.stderr << data end channel.on_request('exit-status') do |channel, data| result.status = data.read_long end channel.on_request('exit-signal') do |channel, data| result.status = 255 end end results << result end ssh.loop end end rescue Net::SSH::HostKeyMismatch => exception exception.remember_host! sleep 0.2 retry end results end |