Class: ExecRemote::RemoteExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/exec_remote/remote_executor.rb

Class Method Summary collapse

Class Method Details

.connect_and_execute(command, host, user, pass = nil) ⇒ Object



7
8
9
10
11
# File 'lib/exec_remote/remote_executor.rb', line 7

def self.connect_and_execute(command, host, user, pass = nil)
  ssh_connect(host, user, pass) do |ssh|
    execute(ssh, command)
  end
end

.execute(ssh_connection, command) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exec_remote/remote_executor.rb', line 23

def self.execute(ssh_connection, command)
  ssh_connection.open_channel do |channel|
    channel.request_pty do |ch, success|
      if success
        ch.exec "sh -c '#{command}'"
      else
        ch.close
      end

      ch.on_data do |c, data|
        print data
      end

      ch.on_extended_data do |c, type, data|
        print data
      end

      ch.on_close { puts "Remote Task Completed" }
    end
  end
end

.ssh_connect(host, user, password, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/exec_remote/remote_executor.rb', line 13

def self.ssh_connect(host, user, password, &block)
  puts "Connecting.."
  ::Net::SSH.start(host, user, :password => password, :config=>false, :paranoid=>false) do |ssh|
    yield(ssh)
  end
rescue Net::SSH::AuthenticationFailed => error
  puts "Authentication with server failed." 
  raise error
end