Class: SSHLine

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/util/ssh.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user = nil) ⇒ SSHLine

Returns a new instance of SSHLine.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbbt/util/ssh.rb', line 5

def initialize(host, user = nil)
  @host = host
  @user = user

  @ssh = Net::SSH.start(@host, @user)

  @ch = @ssh.open_channel do |ch|
    ch.exec 'bash'
  end

  @ch.on_data do |_,data|
    if m = data.match(/DONECMD: (\d+)\n/)
      @exit_status = m[1].to_i
      @output << data.sub(m[0],'')
      serve_output 
    else
      @output << data
    end
  end

  @ch.on_extended_data do |_,c,err|
    STDERR.write err 
  end
end

Class Method Details

.open(host, user = nil) ⇒ Object



65
66
67
# File 'lib/rbbt/util/ssh.rb', line 65

def self.open(host, user = nil)
  @connections[[host, user]] ||= SSHLine.new host, user
end

.ruby(server, script) ⇒ Object



73
74
75
# File 'lib/rbbt/util/ssh.rb', line 73

def self.ruby(server, script)
  open(server).ruby(script)
end

.run(server, cmd) ⇒ Object



69
70
71
# File 'lib/rbbt/util/ssh.rb', line 69

def self.run(server, cmd)
  open(server).cmd(cmd)
end

Instance Method Details

#cmd(command) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/util/ssh.rb', line 40

def cmd(command)
  send_cmd(command)
  @ssh.loop{ ! @complete_output}
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, command
  end
end

#ruby(script) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rbbt/util/ssh.rb', line 50

def ruby(script)
  @output = ""
  @complete_output = false
  cmd = "ruby -e \"#{script.gsub('"','\\"')}\"\n"
  @ch.send_data(cmd)
  @ch.send_data("echo DONECMD: $?\n")
  @ssh.loop{ !@complete_output }
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, "Ruby script:\n#{script}"
  end
end

#send_cmd(command) ⇒ Object



30
31
32
33
34
# File 'lib/rbbt/util/ssh.rb', line 30

def send_cmd(command)
  @output = ""
  @complete_output = false
  @ch.send_data(command+"\necho DONECMD: $?\n")
end

#serve_outputObject



36
37
38
# File 'lib/rbbt/util/ssh.rb', line 36

def serve_output
  @complete_output = true
end