Class: IPAddr

Inherits:
Object show all
Defined in:
lib/jss/ruby_extensions/ipaddr.rb

Overview

A few augmentations to IPAddr handling.

Class Method Summary collapse

Class Method Details

.jss_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.jss_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
72
73
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 59

def self.jss_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

.jss_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.jss_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.



88
89
90
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 88

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

.jss_masked_v4addr(starting, ending) ⇒ IPAddr

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

Examples:

IPAddr.jss_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



43
44
45
# File 'lib/jss/ruby_extensions/ipaddr.rb', line 43

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