Module: JamfRubyExtensions::IPAddr::Utils

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

Class Method Summary collapse

Class Method Details

.j_cidr_from_ends(starting, ending) ⇒ FixNum

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



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

def self.j_cidr_from_ends(starting,ending)

  starting = IPAddr.new(starting) unless starting.kind_of? IPAddr
  ending = IPAddr.new(ending) unless ending.kind_of? 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
  return 32 - num_addrs.to_s(2).length + 1

end

.j_ending_address(starting, cidr) ⇒ IPAddr

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>

Parameters:

  • starting (Strings, IPAddr)

    the starting IP address

  • cidr (FixNum)

    the CIDR mask

Returns:

  • (IPAddr)

    the ending IP address of the range.



87
88
89
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 87

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

.j_masked_v4addr(starting, ending) ⇒ IPAddr

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>

Parameters:

  • starting (Strings, IPAddr)

    the starting IP address

  • ending (Strings, IPAddr)

    the ending IP address

Returns:

  • (IPAddr)

    the IP address range represented as a masked IPv4 address



42
43
44
# File 'lib/jamf/ruby_extensions/ipaddr/utils.rb', line 42

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