Module: JamfRubyExtensions::IPAddr::Utils

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

Instance Method Summary collapse

Instance Method Details

#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


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 41

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

#j_ending_address(starting, cidr) ⇒ IPAddr Also known as: jss_ending_address

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.

Examples:

IPAddr.j_ending_address '10.0.0.0', 24 # => #<IPAddr: IPv4:10.0.0.255>


69
70
71
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 69

def j_ending_address(starting, cidr)
  IPAddr.new("#{starting}/#{cidr}").to_range.max
end

#j_masked_v4addr(starting, ending) ⇒ IPAddr Also known as: jss_masked_v4addr

Convert starting and ending IPv4 IP addresses (either Strings or IPAddrs) into a single masked IPv4 IPAddr

Examples:

IPAddr.j_masked_v4addr '10.0.0.0', '10.0.0.255' # => #<IPAddr: IPv4:10.0.0.0/255.255.255.0>


24
25
26
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 24

def j_masked_v4addr(starting, ending)
  IPAddr.new "#{starting}/#{j_cidr_from_ends(starting, ending)}"
end