Class: Fog::SSH::Real
- Inherits:
-
Object
- Object
- Fog::SSH::Real
- Defined in:
- lib/fog/core/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.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fog/core/ssh.rb', line 38 def initialize(address, username, ) require 'net/ssh' key_manager = Net::SSH::Authentication::KeyManager.new(nil, ) unless [:key_data] || [:keys] || [:password] || key_manager.agent raise ArgumentError.new(':key_data, :keys, :password or a loaded ssh-agent is required to initialize SSH') end @address = address @username = username @options = { :paranoid => false }.merge() end |
Instance Method Details
#run(commands) ⇒ Object
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 |
# File 'lib/fog/core/ssh.rb', line 52 def run(commands) commands = [*commands] results = [] begin Net::SSH.start(@address, @username, @options) do |ssh| commands.each do |command| escaped_command = command.sub(/'/, %{'"'"'}) result = Result.new(escaped_command) ssh.open_channel do |ssh_channel| ssh_channel.request_pty ssh_channel.exec(%{bash -lc '#{escaped_command}'}) do |channel, success| unless success raise "Could not execute command: #{command.inspect}" end channel.on_data do |ch, data| result.stdout << data end channel.on_extended_data do |ch, type, data| next unless type == 1 result.stderr << data end channel.on_request('exit-status') do |ch, data| result.status = data.read_long end channel.on_request('exit-signal') do |ch, data| result.status = 255 end end end ssh.loop results << result end end rescue Net::SSH::HostKeyMismatch => exception exception.remember_host! sleep 0.2 retry end results end |