Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the opposite of a CIDR Range

Tags:

r

ip

cidr

I'd like to make a list of CIDR ranges that represent the addresses outside of the CIDR range I specify.

As a simplified example:

If I have the IP ranges from 8.8.8.8 to 8.8.8.10, I would be able to express this with CIDR ranges 8.8.8.8/31 and 8.8.8.10/32. But how could I express the opposite of these ranges in CIDR notation?

I'm new to CIDR so if this kind of tool exists already in a popular format please excuse my question.

As an additional note, I would like to eventually implement this filtering in R so if you can express your answer using R that would be preferable but I'm really mostly interested in how to build the algorithm to solve this kind of problem.

like image 568
cylondude Avatar asked May 10 '16 16:05

cylondude


People also ask

How do you find the prefix of CIDR?

How many addresses does a CIDR block represent? You calculate 2 32-prefix , where prefix is the number after the slash. For example, /29 contains 232-29=23=8 addresses. Here's a quick table that you can reference for the most CIDR blocks.

How do I get IP range from CIDR?

The formula to calculate the number of assignable IP address to CIDR networks is similar to classful networking. Subtract the number of network bits from 32. Raise 2 to that power and subtract 2 for the network and broadcast addresses. For example, a /24 network has 232-24 - 2 addresses available for host assignment.

How do you interpret CIDR range?

An easy way to understand CIDR is to think of the notation on the end as the amount of bits that will be allocated to the network. In this example “/24” would mean that the first 24 bits are allocated to the network (10101110.00010000. 00000000) and the remaining 8 bits would be allocated to the host (. 00000000).

Is subnet and CIDR same?

CIDR is based on a concept called subnetting. Subnetting allows you to take a class, or block of IP addresses and further chop it up into smaller blocks or groups of IPs. CIDR and subnetting are virtually the same thing.


1 Answers

Basically, if you have a range (X, Y) of IP addresses and want to represent all IP addresses that are not in (X, Y) then it can be expressed as two ranges: (0.0.0.0, X-1) and (Y+1, 255.255.255.255).

Then you just convert the two ranges into CIDRs. A range may result in multiple CIDRs.

In the specific example you want ranges: (0.0.0.0, 8.8.8.7) and (8.8.8.11, 255.255.255.255). I have no idea of how to do this in R but here is a handy calculator here: http://www.ipaddressguide.com/cidr

(0.0.0.0, 8.8.8.7):

  • 0.0.0.0/5
  • 8.0.0.0/13
  • 8.8.0.0/21
  • 8.8.8.0/29

(8.8.8.11, 255.255.255.255):

  • 8.8.8.11/32

  • 8.8.8.12/30

  • 8.8.8.16/28
  • 8.8.8.32/27
  • 8.8.8.64/26
  • 8.8.8.128/25
  • 8.8.9.0/24
  • 8.8.10.0/23
  • 8.8.12.0/22
  • 8.8.16.0/20
  • 8.8.32.0/19
  • 8.8.64.0/18
  • 8.8.128.0/17
  • 8.9.0.0/16
  • 8.10.0.0/15
  • 8.12.0.0/14
  • 8.16.0.0/12
  • 8.32.0.0/11
  • 8.64.0.0/10
  • 8.128.0.0/9
  • 9.0.0.0/8
  • 10.0.0.0/7
  • 12.0.0.0/6
  • 16.0.0.0/4
  • 32.0.0.0/3
  • 64.0.0.0/2
  • 128.0.0.0/1
like image 76
Amit Kulkarni Avatar answered Oct 12 '22 14:10

Amit Kulkarni