Module: Nexpose::HostOrIP
- Defined in:
- lib/nexpose/util.rb
Overview
Function module for dealing with String to HostName|IPRange conversions.
Class Method Summary collapse
-
.convert(asset) ⇒ IPRange|HostName
Convert a host or IP address to the corresponding HostName or IPRange class.
-
.parse(xml) ⇒ Array[HostName|IPRange]
Parse a REXML::Document or REXML::Element for any hosts listed and convert them to HostName and IPRange objects.
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.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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. =~ /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.}" 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.
73 74 75 76 77 78 79 80 81 82 83 |
# 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 |