Class: LegoEv3::SSHScript

Inherits:
Object
  • Object
show all
Defined in:
lib/connection/ssh_command.rb

Instance Method Summary collapse

Constructor Details

#initialize(ssh_config, command) ⇒ SSHScript



5
6
7
8
# File 'lib/connection/ssh_command.rb', line 5

def initialize(ssh_config, command)
  @ssh_config = ssh_config
  @command = command
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/connection/ssh_command.rb', line 10

def run
  puts "Connecting to brick..."

  Net::SSH::Simple.sync(@ssh_config) do
    buffer = ''

    puts "Opening Bash session..."

    ssh('ev3', '/bin/sh') do |event, console, d|
      case event
        when :start
          puts "Executing #{@command} locally..."

          console.send_data(@command)
          console.eof!
        when :stdout
          buffer << d
          while line = buffer.slice!(/(.*)\r?\n/) do puts line end
          :no_append
        when :stderr
          while line = buffer.slice!(/(.*)\r?\n/) do puts line end
          :no_append
        when :exit_code
          puts d
        when :exit_signal
          puts d
          :no_raise
        when :finish
          puts "Done."
      end
    end
  end
end