Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to generate IP addresses from a CIDR address using Python?
In this article, we will learn how to generate IP addresses from a CIDR (Classless Inter-Domain Routing) address using Python's built-in ipaddress module. CIDR notation represents a network address and its subnet mask, allowing us to define IP address ranges efficiently.
What is CIDR Notation?
CIDR notation combines an IP address with a prefix length (e.g., 192.168.1.0/24). The number after the slash indicates how many bits are used for the network portion, determining the range of available host addresses.
Using IPv4Network
The ipaddress.ip_network() function creates a network object from CIDR notation, allowing us to iterate through all available IP addresses ?
import ipaddress
# Create IPv4 network from CIDR address
network = ipaddress.ip_network('192.168.1.0/29')
print("IPv4 addresses in 192.168.1.0/29:")
for ip in network:
print(ip)
The output of the above code is ?
IPv4 addresses in 192.168.1.0/29: 192.168.1.0 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 192.168.1.6 192.168.1.7
Using IPv6Network
The same approach works for IPv6 addresses with their longer notation ?
import ipaddress
# Create IPv6 network from CIDR address
ipv6_network = ipaddress.ip_network('2001:db8::/125')
print("IPv6 addresses in 2001:db8::/125:")
for ip in ipv6_network:
print(ip)
The output of the above code is ?
IPv6 addresses in 2001:db8::/125: 2001:db8:: 2001:db8::1 2001:db8::2 2001:db8::3 2001:db8::4 2001:db8::5 2001:db8::6 2001:db8::7
Accessing Specific IP Addresses
You can access individual IP addresses using indexing, similar to working with lists ?
import ipaddress
network = ipaddress.ip_network('10.0.0.0/30')
print("First IP address:", network[0])
print("Second IP address:", network[1])
print("Last IP address:", network[-1])
print("Total hosts:", network.num_addresses)
The output of the above code is ?
First IP address: 10.0.0.0 Second IP address: 10.0.0.1 Last IP address: 10.0.0.3 Total hosts: 4
Practical Example: Network Analysis
Here's a practical example showing network information and host generation ?
import ipaddress
def analyze_network(cidr):
network = ipaddress.ip_network(cidr)
print(f"Network: {network}")
print(f"Network address: {network.network_address}")
print(f"Broadcast address: {network.broadcast_address}")
print(f"Netmask: {network.netmask}")
print(f"Total addresses: {network.num_addresses}")
print("\nFirst 5 host addresses:")
for i, ip in enumerate(network):
if i < 5:
print(f" {ip}")
else:
break
# Analyze a small network
analyze_network('172.16.10.0/28')
The output of the above code is ?
Network: 172.16.10.0/28 Network address: 172.16.10.0 Broadcast address: 172.16.10.15 Netmask: 255.255.255.240 Total addresses: 16 First 5 host addresses: 172.16.10.0 172.16.10.1 172.16.10.2 172.16.10.3 172.16.10.4
Key Methods and Properties
| Method/Property | Description | Example |
|---|---|---|
network_address |
First address in network | network.network_address |
broadcast_address |
Last address in network | network.broadcast_address |
num_addresses |
Total addresses in network | network.num_addresses |
hosts() |
Generator for host addresses only | list(network.hosts()) |
Conclusion
Python's ipaddress module provides a powerful and simple way to work with CIDR addresses. Use ip_network() to create network objects and iterate through all available IP addresses for network planning and analysis.
