| Module | NetAddr |
| In: |
lib/tree.rb
lib/methods.rb lib/netaddr.rb lib/eui.rb lib/validation_shortcuts.rb lib/ip_math.rb lib/cidr.rb lib/cidr_shortcuts.rb |
Copyleft (c) 2006 Dustin Spinhirne
Licensed under the same terms as Ruby, No Warranty is provided.
Convert an Integer representing a binary netmask into an Integer representing the number of bits in that netmask.
Example: NetAddr.i_to_bits(0xfffffffe) => 31 NetAddr.i_to_bits(0xffffffffffffffff0000000000000000) => 64
Convert an Integer into an IP address. This method will attempt to auto-detect the IP version if not provided, however, a slight speed increase is realized if version is provided.
Example: NetAddr.i_to_ip(3232235906) => "192.168.1.130" NetAddr.i_to_ip(0xffff0000000000000000000000000001, :Version => 6) => "ffff:0000:0000:0000:0000:0000:0000:0001"
:Version -- IP version - Integer (optional) :IPv4Mapped -- if true, unpack IPv6 as an IPv4 mapped address (optional)
Convert IP addresses into an Integer. This method will attempt to auto-detect the IP version if not provided, however a slight speed increase is realized if version is provided.
Example:
NetAddr.ip_to_i('192.168.1.1') => 3232235777
NetAddr.ip_to_i('ffff::1', :Version => 6) => 340277174624079928635746076935438991361
NetAddr.ip_to_i('::192.168.1.1') => 3232235777
:Version -- IP version - Integer
Given a list of CIDR addresses or NetAddr::CIDR objects, merge (summarize) them in the most efficient way possible. Summarization will only occur when the newly created supernets will not result in the ‘creation’ of new IP space. For example the following blocks (192.168.0.0/24, 192.168.1.0/24, and 192.168.2.0/24) would be summarized into 192.168.0.0/23 and 192.168.2.0/24 rather than into 192.168.0.0/22
I have designed this with enough flexibility so that you can pass in CIDR addresses that arent even related (ex. 192.168.1.0/26, 192.168.1.64/27, 192.168.1.96/27 10.1.0.0/26, 10.1.0.64/26) and they will be merged properly (ie 192.168.1.0/25, and 10.1.0.0/25 would be returned).
If the :Objectify option is enabled, then any summary addresses returned will contain the original CIDRs used to create them within the tag value :Subnets (ie. cidr_x.tag[:Subnets] would be an Array of the CIDRs used to create cidr_x)
Example:
cidr1 = NetAddr::CIDR.create('192.168.1.0/27')
cidr2 = NetAddr::CIDR.create('192.168.1.32/27')
NetAddr.merge([cidr1,cidr2])
ip_net_range = NetAddr.range('192.168.35.0','192.168.39.255',:Inclusive => true, :Objectify => true)
NetAddr.merge(ip_net_range, :Objectify => true)
:Objectify -- if true, return NetAddr::CIDR objects :Short -- if true, return IPv6 addresses in short-hand notation
Given the number of IP addresses required in a subnet, return the minimum netmask (bits by default) required for that subnet. IP version is assumed to be 4 unless specified otherwise.
Example: NetAddr.minimum_size(14) => 28 NetAddr.minimum_size(65536, :Version => 6) => 112
:Extended -- If true, then return the netmask, as a String, in extended format (IPv4 only y.y.y.y) :Version -- IP version - Integer
Convert IP netmask into an Integer. Netmask may be in either CIDR (/yy) or extended (y.y.y.y) format. CIDR formatted netmasks may either be a String or an Integer. IP version defaults to 4. It may be necessary to specify the version if an IPv6 netmask of /32 or smaller is provided.
Example:
NetAddr.netmask_to_i('255.255.255.0') => 4294967040
NetAddr.netmask_to_i('24') => 4294967040
NetAddr.netmask_to_i(24) => 4294967040
NetAddr.netmask_to_i('/24') => 4294967040
NetAddr.netmask_to_i('32', :Version => 6) => 340282366841710300949110269838224261120
:Version -- IP version - Integer
Given two CIDR addresses or NetAddr::CIDR objects of the same version, return all IP addresses between them. NetAddr.range will use the original IP address passed during the initialization of the NetAddr::CIDR objects, or the IP address portion of any CIDR addresses passed. The default behavior is to be non-inclusive (don‘t include boundaries as part of returned data).
Example:
lower = NetAddr::CIDR.create('192.168.35.0')
upper = NetAddr::CIDR.create('192.168.39.255')
NetAddr.range(lower,upper, :Limit => 10, :Bitstep => 32)
NetAddr.range('192.168.35.0','192.168.39.255', :Inclusive => true)
NetAddr.range('192.168.35.0','192.168.39.255', :Inclusive => true, :Size => true)
:Bitstep -- enumerate in X sized steps - Integer :Inclusive -- if true, include boundaries in returned data :Limit -- limit returned list to X number of items - Integer :Objectify -- if true, return CIDR objects :Short -- if true, return IPv6 addresses in short-hand notation :Size -- if true, return the number of addresses in this range, but not the addresses themselves
If you do not need all of the fancy options in this method, then please consider using the standard Ruby Range class as shown below.
Example:
start = NetAddr::CIDR.create('192.168.1.0')
fin = NetAddr::CIDR.create('192.168.2.3')
(start..fin).each {|addr| puts addr.desc}
Take a standard IPv6 address and format it in short-hand notation. The address should not contain a netmask.
Example:
NetAddr.shorten('fec0:0000:0000:0000:0000:0000:0000:0001') => "fec0::1"
Sort a list of CIDR addresses or NetAddr::CIDR objects,
Example:
cidr1 = NetAddr::CIDR.create('192.168.1.32/27')
cidr2 = NetAddr::CIDR.create('192.168.1.0/27')
NetAddr.sort([cidr1,cidr2])
NetAddr.sort(['192.168.1.32/27','192.168.1.0/27','192.168.2.0/24'], :Desc => true)
:ByMask -- if true, sorts based on the netmask length :Desc -- if true, return results in descending order
Given a list of CIDR addresses or NetAddr::CIDR objects, return only the top-level supernet CIDR addresses.
If the :Objectify option is enabled, then returned CIDR objects will store the more specific CIDRs (i.e. subnets of those CIDRs) within the tag value :Subnets For example, cidr_x.tag[:Subnets] would be an Array of CIDR subnets of cidr_x.
Example: NetAddr.supernets(['192.168.0.0', '192.168.0.1', '192.168.0.0/31'])
:Objectify -- if true, return NetAddr::CIDR objects :Short -- if true, return IPv6 addresses in short-hand notation
Take an IPv6 address in short-hand format, and expand it into standard notation. The address should not contain a netmask.
Example:
NetAddr.unshorten('fec0::1') => "fec0:0000:0000:0000:0000:0000:0000:0001"
Validate an IP address. The address should not contain a netmask. This method will attempt to auto-detect the IP version if not provided, however a slight speed increase is realized if version is provided. Raises NetAddr::ValidationError on validation failure.
Example:
NetAddr.validate_ip_addr('192.168.1.1') => true
NetAddr.validate_ip_addr('ffff::1', :Version => 6) => true
NetAddr.validate_ip_addr('::192.168.1.1') => true
NetAddr.validate_ip_addr(0xFFFFFF) => true
NetAddr.validate_ip_addr(2**128-1) => true
NetAddr.validate_ip_addr(2**32-1, :Version => 4) => true
:Version -- IP version - Integer (optional)
Validate IP Netmask. Version defaults to 4 if not specified. Raises NetAddr::ValidationError on validation failure.
Examples:
NetAddr.validate_ip_netmask('/32') => true
NetAddr.validate_ip_netmask(32) => true
NetAddr.validate_ip_netmask(0xffffffff, :Integer => true) => true
:Integer -- if true, the provided Netmask is an Integer mask :Version -- IP version - Integer (optional)
Convert a wildcard IP into a valid CIDR address. Wildcards must always be at the end of the address. Any data located after the first wildcard will be lost. Shorthand notation is prohibited for IPv6 addresses. IPv6 encoded IPv4 addresses are not currently supported.
Examples:
NetAddr.wildcard('192.168.*')
NetAddr.wildcard('192.168.1.*')
NetAddr.wildcard('fec0:*')
NetAddr.wildcard('fec0:1:*')