Module: Nexpose::HostOrIP

Defined in:
lib/nexpose/util.rb

Overview

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

Class Method Summary collapse

Class Method Details

.convert(asset) ⇒ IPRange|HostName

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

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

Parameters:

  • asset (String)

    String representation of an IP or host name.

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nexpose/util.rb', line 56

def convert(asset)
  begin
    # Use IPAddr construtor validation to see if it's an IP.
    IPAddr.new(asset)
    IPRange.new(asset)
  rescue ArgumentError => e
    if e.message == 'invalid address'
      HostName.new(asset)
    else
      raise "Unable to parse asset: '#{asset}'. #{e.message}"
    end
  end
end

.parse(xml) ⇒ Array[HostName|IPRange]

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

Parameters:

  • xml (REXML::Document|REXML::Element)

    REXML class potentially containing host references.

Returns:



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/nexpose/util.rb', line 77

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