I'm dealing with this at work right now, and it was really unintuitive for me at first.
So, I'm breaking it down as much as possible for those of you who might not be very familiar with IP addresses.
192.168.0.0
192.168.0.0
is just an ip address :)
Suffix with / and a number
192.168.0.0/32
is a range of IP addresses192.168.0.0/31
is also a range of IP addresses192.168.0.0/30
is also a range of IP addresses192.168.0.0/29
is also a range of IP addresses192.168.0.0/28
is also a range of IP addresses- and so on
The number we put after the /
ranges from 0 to 32
Finding the IPs inside this range
Let's first find the number of IP addresses inside this range.
Let's say we have 192.168.0.0/30
:
number of ip addresses = 2 ^ (32–30) which is 2 ^ 2 = 4 The ip addresses are thus 192.168.0.0, 192.168.0.1, 192.168.0.2 and 192.168.0.3
Let's say we have 192.168.0.0/29
:
number of ip addresses = 2 ^ (32–29) which is 2 ^ 3 = 8 The ip addresses are thus 192.168.0.0, 192.168.0.1, 192.168.0.2 … 192.168.0.7
Let's say we have 192.168.0.1/28
:
number of ip address = 2 ^ (32–28) which is 2 ^ 4 = 16 The ip addresses are thus 192.168.0.0, 192.168.0.1, 192.168.0.2 … 192.168.0.15
Note — I find this notation strange and unintuitive, but since it's widely used in the industry, I guess it's not my place to challenge it.
Finding the IP ranges using Python
We can automatically do this using the built-in ipaddress
module
import ipaddress from pprint import pprint x = list(ipaddress.ip_network('192.168.0.0/30', False)) # there are 4 ip addresses in this range pprint(x) ''' [IPv4Address('192.168.0.0'), IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2'), IPv4Address('192.168.0.3')] '''
^ the False
argument turns off strict mode (which causes an error)
import ipaddress x = list(ipaddress.ip_network('192.168.0.0/28', False)) from pprint import pprint # there are 16 ip addresses in this range pprint(x) ''' [IPv4Address('192.168.0.0'), IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2'), IPv4Address('192.168.0.3'), IPv4Address('192.168.0.4'), IPv4Address('192.168.0.5'), IPv4Address('192.168.0.6'), IPv4Address('192.168.0.7'), IPv4Address('192.168.0.8'), IPv4Address('192.168.0.9'), IPv4Address('192.168.0.10'), IPv4Address('192.168.0.11'), IPv4Address('192.168.0.12'), IPv4Address('192.168.0.13'), IPv4Address('192.168.0.14'), IPv4Address('192.168.0.15')] '''
The "/24" in "192.168.0.0/24"
number of ip addresses = 2 ^ (32–24) which is 2 ^ 8 = 256
import ipaddress x = list(ipaddress.ip_network('192.168.0.0/24', False)) from pprint import pprint pprint(x) ''' [IPv4Address('192.168.0.0'), IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2'), IPv4Address('192.168.0.3'), IPv4Address('192.168.0.4'), IPv4Address('192.168.0.5'), IPv4Address('192.168.0.6'), IPv4Address('192.168.0.7'), IPv4Address('192.168.0.8'), IPv4Address('192.168.0.9'), IPv4Address('192.168.0.10'), IPv4Address('192.168.0.11'), IPv4Address('192.168.0.12'), IPv4Address('192.168.0.13'), IPv4Address('192.168.0.14'), IPv4Address('192.168.0.15'), IPv4Address('192.168.0.16'), IPv4Address('192.168.0.17'), IPv4Address('192.168.0.18'), IPv4Address('192.168.0.19'), IPv4Address('192.168.0.20'), IPv4Address('192.168.0.21'), IPv4Address('192.168.0.22'), IPv4Address('192.168.0.23'), IPv4Address('192.168.0.24'), IPv4Address('192.168.0.25'), IPv4Address('192.168.0.26'), IPv4Address('192.168.0.27'), IPv4Address('192.168.0.28'), IPv4Address('192.168.0..., IPv4Address('192.168.0.224'), IPv4Address('192.168.0.225'), IPv4Address('192.168.0.226'), IPv4Address('192.168.0.227'), IPv4Address('192.168.0.228'), IPv4Address('192.168.0.229'), IPv4Address('192.168.0.230'), IPv4Address('192.168.0.231'), IPv4Address('192.168.0.232'), IPv4Address('192.168.0.233'), IPv4Address('192.168.0.234'), IPv4Address('192.168.0.235'), IPv4Address('192.168.0.236'), IPv4Address('192.168.0.237'), IPv4Address('192.168.0.238'), IPv4Address('192.168.0.239'), IPv4Address('192.168.0.240'), IPv4Address('192.168.0.241'), IPv4Address('192.168.0.242'), IPv4Address('192.168.0.243'), IPv4Address('192.168.0.244'), IPv4Address('192.168.0.245'), IPv4Address('192.168.0.246'), IPv4Address('192.168.0.247'), IPv4Address('192.168.0.248'), IPv4Address('192.168.0.249'), IPv4Address('192.168.0.250'), IPv4Address('192.168.0.251'), IPv4Address('192.168.0.252'), IPv4Address('192.168.0.253'), IPv4Address('192.168.0.254'), IPv4Address('192.168.0.255')] '''
^ that's a lot of IP addresses
Conclusion
I won’t dive into the theory behind IP addresses and what they represent (that would take a few more articles), but I hope this was helpful and easy to understand.