module Nexpose::HostOrIP

Function module for dealing with String to HostName|IPRange conversions.

Public Instance Methods

convert(asset) click to toggle source

Convert a host or IP address to the corresponding HostName or IPRange class.

If the String cannot be converted, it will raise an error.

@param [String] asset String representation of an IP or host name. @return [IPRange|HostName] Valid class, if it can be converted.

# File lib/nexpose/util.rb, line 52
def convert(asset)
  ips = asset.split('-').map(&:strip)
  IPAddr.new(ips[0])
  IPAddr.new(ips[1]) if ips[1]
  IPRange.new(ips[0], ips[1])
rescue ArgumentError => e
  if e.message =~ /invalid address/
    # Try to parse the the asset as a hostname if the IP address conversion fails
    HostName.new(asset)
  else
    raise "Unable to parse asset: '#{asset}'. #{e.message}"
  end
end
parse(xml) click to toggle source

Parse a REXML::Document or REXML::Element for any hosts listed and convert them to HostName and IPRange objects.

@param [REXML::Document|REXML::Element] xml REXML class potentially

containing host references.

@return [Array] Collection of parsed hosts.

# File lib/nexpose/util.rb, line 73
def parse(xml)
  coll = []
  xml.elements.each('//range') do |elem|
    to = elem.attribute('to').nil? ? nil : elem.attribute('to').value
    coll << IPRange.new(elem.attribute('from').value, to)
  end
  xml.elements.each('//host') do |elem|
    coll << HostName.new(elem.text)
  end
  coll
end