module JamfRubyExtensions::IPAddr::Utils
Public Instance Methods
Given starting and ending IPv4 IP addresses (either Strings or IPAddrs) return the CIDR notation routing prefix mask
@param starting[Strings, IPAddr] the starting IP address
@param ending[Strings, IPAddr] the ending IP address
@return [FixNum] the CIDR notation routing prefix mask
@example
IPAddr.j_cidr_from_ends '10.0.0.0', '10.0.0.255' # => 24
# File lib/jamf/ruby_extensions/ipaddr/utils.rb 59 def j_cidr_from_ends(starting,ending) 60 61 starting = IPAddr.new(starting) unless starting.kind_of? IPAddr 62 ending = IPAddr.new(ending) unless ending.kind_of? IPAddr 63 64 ### how many possible addresses in the range? 65 num_addrs = ending.to_i - starting.to_i + 1 66 67 ### convert the number of possible addresses to 68 ### binary then subtract the number of bits from 69 ### the full length of an IPv4 addr 70 ### (32 bits) and that gives the CIDR prefix 71 return 32 - num_addrs.to_s(2).length + 1 72 73 end
Convert a starting address (either String
or IPAddr
) and a CIDR notation routing prefix mask into the IPv4 address of at the end of the range of addresses.
@param starting[Strings, IPAddr] the starting IP address
@param cidr the CIDR mask
@return [IPAddr] the ending IP address of the range.
@example
IPAddr.j_ending_address '10.0.0.0', 24 # => #<IPAddr: IPv4:10.0.0.255>
# File lib/jamf/ruby_extensions/ipaddr/utils.rb 89 def j_ending_address(starting, cidr) 90 IPAddr.new( "#{starting}/#{cidr}").to_range.max 91 end
Convert starting and ending IPv4 IP addresses (either Strings or IPAddrs) into a single masked IPv4 IPAddr
@param starting[Strings, IPAddr] the starting IP address
@param ending[Strings, IPAddr] the ending IP address
@return [IPAddr] the IP address range represented as a masked IPv4 address
@example
IPAddr.j_masked_v4addr '10.0.0.0', '10.0.0.255' # => #<IPAddr: IPv4:10.0.0.0/255.255.255.0>
# File lib/jamf/ruby_extensions/ipaddr/utils.rb 42 def j_masked_v4addr(starting,ending) 43 IPAddr.new "#{starting}/#{self.j_cidr_from_ends(starting,ending)}" 44 end