class Nexpose::IPRange

Object that represents a single IP address or an inclusive range of IP addresses. If to is nil then the from field will be used to specify a single IP Address only.

Attributes

from[RW]

Start of range *Required

to[RW]

End of range *Optional (If nil then IPRange is a single IP Address)

Public Class Methods

new(from, to = nil) click to toggle source

@overload initialize(ip)

@param [#to_s] from the IP single IP address.
@example
  Nexpose::IPRange.new('192.168.1.0')

@overload initialize(start_ip, end_ip)

@param [#to_s] from the IP to start the range with.
@param [#to_s] to the IP to end the range with.
@example
  Nexpose::IPRange.new('192.168.1.0', '192.168.1.255')

@overload initialize(cidr_range)

@param [#to_s] from the CIDR notation IP address range.
@example
  Nexpose::IPRange.new('192.168.1.0/24')
@note The range will not be stripped of reserved IP addresses (such as
  x.x.x.0 and x.x.x.255).

@return [IPRange] an IP address range of one or more addresses.

# File lib/nexpose/site.rb, line 651
def initialize(from, to = nil)
  @from = from
  @to   = to unless from == to

  return unless @to.nil?

  range = IPAddr.new(@from.to_s).to_range
  unless range.one?
    @from = range.first.to_s
    @to   = range.last.to_s
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/nexpose/site.rb, line 678
def <=>(other)
  return 1 unless other.respond_to? :from
  from    = IPAddr.new(@from)
  to      = @to.nil? ? from : IPAddr.new(@to)
  cf_from = IPAddr.new(other.from)
  cf_to   = IPAddr.new(other.to.nil? ? other.from : other.to)
  if cf_to < from
    1
  elsif to < cf_from
    -1
  else # Overlapping
    0
  end
end
==(other) click to toggle source
# File lib/nexpose/site.rb, line 693
def ==(other)
  eql?(other)
end
as_xml() click to toggle source
# File lib/nexpose/site.rb, line 721
def as_xml
  xml = REXML::Element.new('range')
  xml.add_attributes({ 'from' => @from, 'to' => @to })
  xml
end
Also aliased as: to_xml_elem
eql?(other) click to toggle source
# File lib/nexpose/site.rb, line 697
def eql?(other)
  return false unless other.respond_to? :from
  @from == other.from && @to == other.to
end
hash() click to toggle source
# File lib/nexpose/site.rb, line 717
def hash
  to_xml.hash
end
include?(single_ip) click to toggle source
# File lib/nexpose/site.rb, line 702
def include?(single_ip)
  return false unless single_ip.respond_to? :from
  from  = IPAddr.new(@from)
  to    = @to.nil? ? from : IPAddr.new(@to)
  other = IPAddr.new(single_ip)

  if other < from
    false
  elsif to < other
    false
  else
    true
  end
end
size() click to toggle source

Size of the IP range. The total number of IP addresses represented by this range.

@return [Fixnum] size of the range.

# File lib/nexpose/site.rb, line 669
def size
  return 1 if @to.nil?
  from = IPAddr.new(@from)
  to   = IPAddr.new(@to)
  (from..to).to_a.size
end
to_s() click to toggle source
# File lib/nexpose/site.rb, line 732
def to_s
  return from.to_s if to.nil?
  "#{from.to_s} - #{to.to_s}"
end
to_xml() click to toggle source
# File lib/nexpose/site.rb, line 728
def to_xml
  as_xml.to_s
end
to_xml_elem()
Alias for: as_xml