How to Get IPv4/IPv6 Address Range from CIDR in Python?

The following example code shows how to get IPv4 and IPv6 address range from CIDR in Python:


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)
print("Start ip = ", ip_range[0])
print("End ip = ", ip_range[1])
Output:
Start ip = 192.109.82.0
End ip = 192.109.83.255