Method: JamfRubyExtensions::IPAddr::Utils#j_cidr_from_ends

Defined in:
lib/jamf/ruby_extensions/ipaddr/utils.rb

#j_cidr_from_ends(starting, ending) ⇒ FixNum Also known as: jss_cidr_from_ends

Given starting and ending IPv4 IP addresses (either Strings or IPAddrs) return the CIDR notation routing prefix mask

Examples:

IPAddr.j_cidr_from_ends '10.0.0.0', '10.0.0.255' # => 24

Parameters:

  • starting (Strings, IPAddr)

    the starting IP address

  • ending (Strings, IPAddr)

    the ending IP address

Returns:

  • (FixNum)

    the CIDR notation routing prefix mask



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 59

def j_cidr_from_ends(starting, ending)
  starting = IPAddr.new(starting) unless starting.is_a? IPAddr
  ending = IPAddr.new(ending) unless ending.is_a? IPAddr

  ### how many possible addresses in the range?
  num_addrs = ending.to_i - starting.to_i + 1

  ### convert the number of possible addresses to
  ### binary then subtract the number of bits from
  ### the full length of an IPv4 addr
  ### (32 bits) and that gives the CIDR prefix
  32 - num_addrs.to_s(2).length + 1
end