How to Get IPv4/IPv6 Address Range from CIDR in Python?
CIDR is short for Classless Inter-Domain Routing. It is used for allocating IP addresses and for IP routing. The CIDR number usually comes after the IP address and is preceded by a slash "/". For example, 192.109.82.0 would be expressed as 192.109.82.0/24 using a subnet mask of 255.255.255.0 (which has 24 network bits).
To get the starting and ending IP range from CIDR in Python, we can use Python's built-in module called ipaddress as shown in the example below:
import ipaddress
def get_ip_range(cidr):
net = ipaddress.ip_network(cidr)
return net[0], net[-1]
cidr = "192.109.82.0/23"
ip_range = get_ip_range(cidr)
start_ip = ip_range[0]
end_ip = ip_range[1]
print("Start ip = ", start_ip)
print("End ip = ", end_ip)
Output:
Start ip = 192.109.82.0
End ip = 192.109.83.255
End ip = 192.109.83.255