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:



52
53
54
55
56
57
58
59
60
61
62
63
# 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'
    HostName.new(asset)
  else
    raise "Unable to parse asset: '#{asset}'. #{e.message}"
  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:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nexpose/util.rb', line 72

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