Class: GlTail::Source::SSH

Inherits:
Base
  • Object
show all
Defined in:
lib/gl_tail/sources/ssh.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #name, #parser

Instance Method Summary collapse

Methods inherited from Base

#add_activity, #add_event, #initialize

Methods included from Configurable

#config_rewrite_color, included

Constructor Details

This class inherits a constructor from GlTail::Source::Base

Instance Method Details

#do_tail(file, command) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gl_tail/sources/ssh.rb', line 79

def do_tail( file, command )
  @session.open_channel do |channel|
    puts "Channel opened on #{@session.host}...\n" if($VRB > 0 || $DBG > 0)

    @buffer = ""
    channel.request_pty :want_reply => true

    channel.on_data do |ch, data|
      @buffer << data
      parse_line(data)
    end

    channel.on_success do |ch|
      channel.exec "#{command} #{file}  "
    end

    channel.on_failure do |ch|
      ch.close
    end

    channel.on_extended_data do |ch, data|
      puts "STDERR: #{data}\n"
    end

    channel.on_close do |ch|
      ch[:closed] = true
    end

    puts "Pushing #{host}\n" if($VRB > 0 || $DBG > 0)
    @channels.push(channel)
  end
end

#initObject



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
43
44
45
46
47
48
49
50
# File 'lib/gl_tail/sources/ssh.rb', line 17

def init

  @channels = []

  puts "Connecting to #{host}..." if($VRB > 0 || $DBG > 0)

  session_options = { }
  session_options[:port] = port if port
  session_options[:keys] = keys if keys
  session_options[:verbose] = :debug if $DBG > 1

  begin
    if password
      session_options[:auth_methods] = [ "password","keyboard-interactive" ]

      @session = Net::SSH.start(host, user, password, session_options)
    else
      @session = Net::SSH.start(host, user, session_options)
    end
  rescue SocketError => e
    puts "!!! Could not connect to #{host}. Check to make sure that this is the correct url."
    puts $! if $DBG > 0
    exit
  rescue Net::SSH::AuthenticationFailed => e
    puts "!!! Could not authenticate on #{host}. Make sure you have set the username and password correctly. Or if you are using SSH keys make sure you have not set a password."
    puts $! if $DBG > 0
    exit
  end

  # FIXME: add support for multiple files (eg. write files accessor)
  do_tail(files, command)

  @session.connection.process
end

#parse_line(data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gl_tail/sources/ssh.rb', line 60

def parse_line(data)
  @buffer.gsub(/\r\n/,"\n").gsub(/\n/, "\n\n").each("") do |line|

    unless line.include? "\n\n"
      @buffer = "#{line}"
      next
    end

    line.gsub!(/\n\n/, "\n")
    line.gsub!(/\n\n/, "\n")

    puts "#{host}[#{user}]: #{line}" if $DBG > 0

    parser.parse(line)
  end

  @buffer = "" if @buffer.include? "\n"
end

#processObject



52
53
54
# File 'lib/gl_tail/sources/ssh.rb', line 52

def process
  @session.connection.process(true)
end

#updateObject



56
57
58
# File 'lib/gl_tail/sources/ssh.rb', line 56

def update
  @channels.each { |ch| ch.connection.ping! }
end