class NetCrawl::Output::Dot

Constants

DEFAULT_COLOR
INDENT

Public Class Methods

new(output) click to toggle source
# File lib/netcrawl/output/dot.rb, line 32
def initialize output
  @output      = output
  @connections = []
  @peers       = @output.peers
  @resolve     = @output.resolve
end
output(output) click to toggle source
# File lib/netcrawl/output/dot.rb, line 6
def self.output output
  new(output).to_s
end

Public Instance Methods

to_s() click to toggle source
# File lib/netcrawl/output/dot.rb, line 10
def to_s
  str = "graph NetCrawl {\n"
  @output.to_list.each do |host|
    host_label = label(host)
    str << INDENT + id(host) + "[label=\"#{host_label}\" color=\"#{color(host_label)}\"]\n"
    if @peers.has_key? host
      @peers[host].each do |peer|
        peer_name = @resolve ? peer.name : peer.ip
        next if not CFG.dot.bothlinks and @connections.include?([peer_name, host].sort)
        @connections << [peer_name, host].sort
        labels = ''
        labels = "[headlabel=\"#{peer.dst.to_s}\" taillabel=\"#{peer.src.to_s}\"]" if CFG.dot.linklabel
        str << INDENT + INDENT + id(host) + ' -- ' + id(peer_name) + labels + "\n"
      end
    end
  end
  str << "}\n"
  str
end

Private Instance Methods

color(host) click to toggle source
# File lib/netcrawl/output/dot.rb, line 60
def color host
  color = nil
  CFG.dot.color.each do |re, clr|
    if host.match re
      color = clr
      break
    end
  end
  color or DEFAULT_COLOR
end
id(host) click to toggle source
# File lib/netcrawl/output/dot.rb, line 39
def id host
  host = host.gsub(/[-.]/, '_')
  '_' + host
end
label(wanthost) click to toggle source
# File lib/netcrawl/output/dot.rb, line 44
def label wanthost
  label = nil
  return wanthost if CFG.ipname == true
  @peers.each do |host, peers|
    peers.each do |peer|
      gothost = @resolve ? peer.name : peer.ip
      if wanthost == gothost
        label = peer.raw_name
        break
      end
    end
    break if label
  end
  label or wanthost
end