Sha256: 402fd610d5b6c64391df4a84d8dc409867ccad14468af03de05ce07a8dd3de35

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require "net/ssh"

# The main SmartMachine SSH driver
module SmartMachine
	class SSH < SmartMachine::Base
		def initialize
		end

		def run(*commands)
			commands.flatten!
			Net::SSH.start(SmartMachine.credentials.machine[:address], SmartMachine.credentials.machine[:username], { port: SmartMachine.credentials.machine[:port], password: SmartMachine.credentials.machine[:password] }) do |ssh|
				channel = ssh.open_channel do |channel, success|
					channel.request_pty do |channel, success|
						channel.exec commands.join(';') do |channel, success|
							raise "Could not execute command" unless success

							channel.on_data do |channel, data|
								$stdout.print data

								if data =~ /^\[sudo\] password for /
									channel.send_data "#{SmartMachine.credentials.machine[:password]}\n"
								end
							end

							channel.on_extended_data do |channel, type, data|
								$stderr.print data
							end

							channel.on_close do |channel|
								# puts "done!"
							end
						end
					end
				end
				channel.wait
			end
		end

		def login
			exec "ssh #{SmartMachine.credentials.machine[:username]}@#{SmartMachine.credentials.machine[:address]}"
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smartmachine-0.8.0 lib/smart_machine/ssh.rb
smartmachine-0.7.0 lib/smart_machine/ssh.rb