Ip Range to Cidr in Ruby/Rails

IP Range to CIDR in Ruby/Rails?

Well, to get the CIDR notation of a range, you need an IP and the number of network bits (calculated from the netmask).

To enumerate the addresses of a given range, you can use the NetAddr (< 2.x) gem.

p NetAddr::CIDR.create('192.168.1.0/24').enumerate
=> ['192.168.1.0', '192.168.1.1', '192.168.1.2'... '192.168.1.255']

You can also calculate the bits from the netmask on the fly:

mask_int = NetAddr.netmask_to_i('255.255.255.0')
p NetAddr.mask_to_bits(mask_int)
=> 24

And to create a range based on two IPs:

lower = NetAddr::CIDR.create('192.168.1.1')
upper = NetAddr::CIDR.create('192.168.1.10')
p NetAddr.range(lower, upper)
=> ['192.168.1.2', '192.168.1.3'... '192.168.1.9']

So now that you can create a CIDR range, you can check to see if an IP is a part of it:

cidr = NetAddr::CIDR.create('192.168.1.0/24')
p cidr.contains?('192.168.1.10')
=> true

How to convert IP range to a single CIDR using Ruby

Found it!

require 'netaddr'

startip = '250.154.64.0'
endip = '250.154.64.255'

ip_net_range = NetAddr.range(startip, endip, :Inclusive => true, :Objectify => true)
cidrs = NetAddr.merge(ip_net_range, :Objectify => true)

puts cidrs

output: 250.154.64.0/24

Rails how to validate subnet mask before ip range calculation

ruby ipaddr

require 'ipaddr'
net1 = IPAddr.new("192.168.2.0/24")
net2 = IPAddr.new("192.168.2.100")
net3 = IPAddr.new("192.168.3.0")
p net1.include?(net2) #=> true
p net1.include?(net3) #=> false

Netmask to CIDR in ruby

Here is the quick and dirty way

require 'ipaddr'
puts IPAddr.new("255.255.255.0").to_i.to_s(2).count("1")

There should be proper function for that, I couldn't find that, so I just count "1"

If you're going to be using the function in a number of places and don't mind monkeypatching, this could help:

IPAddr.class_eval
def to_cidr
"/" + self.to_i.to_s(2).count("1")
end
end

Then you get

IPAddr.new('255.255.255.0').to_cidr
# => "/24"

Ruby: How to convert IP range to array of IP's

Use the Ruby standard library IPAddr

# I would suggest naming your function using underscore rather than camelcase
# because of Ruby naming conventions
#
require 'ipaddr'

def convert_ip_range(start_ip, end_ip)
start_ip = IPAddr.new(start_ip)
end_ip = IPAddr.new(end_ip)

# map to_s if you like, you can also call to_a,
# IPAddrs have some neat functions regarding IPs,
# be sure to check them out
#
(start_ip..end_ip).map(&:to_s)
end

Creating IP address range in Ruby

require 'ipaddr'
puts IPAddr.new("123.123.0.0/16").to_range.to_a

If you absolutely have to use the ambiguous "IP defines range" data in the question,

require 'ipaddr'
STDIN.read.each_line do |line|
num_zeroes = line[/(\.0)*$/].length / 2
ip_range_string = "#{line.chomp}/#{32 - num_zeroes * 8}"
puts IPAddr.new(ip_range_string).to_range.to_a
end

Find out if an IP is within a range of IPs

>> require "ipaddr"
=> true
>> low = IPAddr.new("62.0.0.0").to_i
=> 1040187392
>> high = IPAddr.new("62.255.255.255").to_i
=> 1056964607
>> ip = IPAddr.new("62.156.244.13").to_i
=> 1050473485
>> (low..high)===ip
=> true

If you are given the network instead of the start and end addresses, it is even simpler

>> net = IPAddr.new("62.0.0.0/8")
=> #<IPAddr: IPv4:62.0.0.0/255.0.0.0>
>> net===IPAddr.new("62.156.244.13")
=> true

IPAddr will also work with IPv6 addresses

>> low = IPAddr.new('1::')
=> #<IPAddr: IPv6:0001:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> high = IPAddr.new('2::')
=> #<IPAddr: IPv6:0002:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> (low..high)===IPAddr.new('1::1')
=> true


Related Topics



Leave a reply



Submit