Class: Netstat

Inherits:
Object
  • Object
show all
Includes:
RunCommand
Defined in:
lib/city_watch/commands/netstat.rb

Instance Method Summary collapse

Methods included from RunCommand

#command_line, #command_line_opts, #command_output, included, #options, #set_opts, #supported?

Instance Method Details

#dataObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/city_watch/commands/netstat.rb', line 7

def data
  headers = false
  output = {}
  command_output.split("\n").each do |line|
    if line[/^Active /,0]
      type = line.split(" ")[1]
    end
    if line[/^Proto /,0]
      fmt = line.split(/ \b/).map {|v| [v.strip.downcase.to_sym,v.length]}
      fmt[fmt.size-1][1] = 99
      next
    end
    next unless fmt
    pkt = {}
    fmt.each do |(name,length)|
      val = line.slice!(0..length-1).strip
      pkt[name] = val
    end
    output[type] ||= []
    output[type] << pkt
  end
  output
end