class Fulmar::Plugin::CORE4::HostFile

Analyses /etc/hosts

Attributes

filename[R]

Public Class Methods

new(filename) click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 8
def initialize(filename)
  @filename = filename
end

Public Instance Methods

check() click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 12
def check
  read_file
  duplicate_ipv4 = find_duplicates(@ipv4_hosts)
  duplicate_ipv6 = find_duplicates(@ipv6_hosts)
  duplicate_ipv4 + duplicate_ipv6
end
find_duplicates(hosts) click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 19
def find_duplicates(hosts)
  hosts.group_by(&:last).select { |_key, value| value.size > 1 }.map(&:first)
end
read_file() click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 23
def read_file
  @ipv4_hosts = []
  @ipv6_hosts = []
  File.open(@filename) do |file|
    until file.eof?
      if (hosts = parse_line(file.gets))
        ip = hosts.shift
        add_hosts(ip, hosts)
      end
    end
  end
end

Protected Instance Methods

add_hosts(ip, hosts) click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 38
def add_hosts(ip, hosts)
  if ip.include?(':') # ipv6
    hosts.each do |host|
      @ipv6_hosts << [ip, host]
    end
  else # ipv4
    hosts.each do |host|
      @ipv4_hosts << [ip, host]
    end
  end
end
parse_line(line) click to toggle source
# File lib/fulmar/plugin/core4/host_file.rb, line 50
def parse_line(line)
  return nil unless line
  line = line.split('#').first.strip
  return nil if line.empty?
  parts = line.gsub(/\s+/, ' ').split(' ')
  return nil if parts.size == 1
  parts
end