How to generate IP addresses from a CIDR address using Python?


In this article, we will learn how to generate IP addresses from the CIDR address.

Methods Used

The following are the various methods to accomplish this task −

  • Generating IPv4 Network addresses

  • Generating IPv6 Network addresses

  • Accessing IP addresses of the CIDR address

Method 1: IPv4Network

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Use the import keyword to Import the ipaddress module.

  • Use the ip_network() function(returns the type of network of the address ) of ipaddress module to get the IP addresses from an input CIDR address(IPv4Network)

  • Use the for loop to traverse through the above IPv4 addresses list

  • Print the current IPv4 address of the list

Example

The following program returns the list of IPv4 addresses from the given CIDR address −

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv4 network address)
netIpv4Address = ipaddress.ip_network('123.45.66.64/27')
print("The Following are the IPv4 Addresses in the given CIDR Address(123.45.66.64/27)")
# traversing through the above IPv4 addresses list
for i in netIpv4Address:
   # printing the current IPv4 address
   print(i)

Output

On execution, the above program will generate the following output −

The Following are the IPv4 Addresses in the given CIDR Address(123.45.66.64/27)
123.45.66.64
123.45.66.65
123.45.66.66
123.45.66.67
123.45.66.68
123.45.66.69
123.45.66.70
123.45.66.71
123.45.66.72
123.45.66.73
123.45.66.74
123.45.66.75
123.45.66.76
123.45.66.77
123.45.66.78
123.45.66.79
123.45.66.80
123.45.66.81
123.45.66.82
123.45.66.83
123.45.66.84
123.45.66.85
123.45.66.86
123.45.66.87
123.45.66.88
123.45.66.89
123.45.66.90
123.45.66.91
123.45.66.92
123.45.66.93
123.45.66.94
123.45.66.95

Method 2: IPv6Network

Give the CIDR address of the IPv6 Network as an argument to the ip_network() function and print all the IPv6 Network of it by traversing through the result.

Example

The following program returns the list of IPv6 addresses from the given CIDR address −

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv6 network address)
netIpv6Address = ipaddress.ip_network('12:3456:78:90ab:cd:ef11:23:30/125')
print("The Following are the IPv6 Addresses in the given CIDR Address(12:3456:78:90ab:cd:ef11:23:30/125)")
# traversing in the above Ipv6Address
for i in netIpv6Address:
   # printing the current Ipv6Address
   print(i)

Output

On execution, the above program will generate the following output −

The Following are the IPv6 Addresses in the given CIDR Address(12:3456:78:90ab:cd:ef11:23:30/125)
12:3456:78:90ab:cd:ef11:23:30
12:3456:78:90ab:cd:ef11:23:31
12:3456:78:90ab:cd:ef11:23:32
12:3456:78:90ab:cd:ef11:23:33
12:3456:78:90ab:cd:ef11:23:34
12:3456:78:90ab:cd:ef11:23:35
12:3456:78:90ab:cd:ef11:23:36
12:3456:78:90ab:cd:ef11:23:37

Method 3: Accessing IP addresses of the CIDR address

We can access the Ip addresses of the given CIDR address by getting all the IP addresses corresponding to it as a list and accessing the list elements using the [] operator i.e index method.

Example

The following program shows how to access the IP addresses of the CIDR address −

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv4 network address)
netIpv4Address = ipaddress.ip_network('123.45.66.64/27')
# accessing the first Ipv4Address from the resultant list
print("First Ipv4Address from the list:", netIpv4Address[0])
 
# accessing the second Ipv4Address from the resultant list
print("Second Ipv4Address from the list:", netIpv4Address[1])
 
# accessing the last Ipv4Address from the resultant list
print("Last Ipv4Address from the list:", netIpv4Address[-1])

Output

On execution, the above program will generate the following output −

First Ipv4Address from the list: 123.45.66.64
Second Ipv4Address from the list: 123.45.66.65
Last Ipv4Address from the list: 123.45.66.95

Conclusion

This article taught us how to extract every IP address from the provided CIDR addresses. We used two approaches, one to get all IPv4 addresses and the other to collect IPv6 addresses. We also showed how to use the [] operator to access the IP addresses after converting them to a list.

Updated on: 24-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements