Planning IP addresses for your local area network

Suppose you have a CIDR network address such as 12:3456:78:90ab:cd:ef01:23:30/125, and you want to generate a range of all the IP addresses that it represents (12:3456:78:90ab:cd:ef01:23:30 to 12:3456:78:90ab:cd:ef01:23:37). The ipaddress module can be easily used to perform such calculations:

 >>> import ipaddress
>>> net6 = ipaddress.ip_network('12:3456:78:90ab:cd:ef01:23:30/125')
>>> net6
IPv6Network('12:3456:78:90ab:cd:ef01:23:30/125')
>>> for ip in net:
... print(ip)
12:3456:78:90ab:cd:ef01:23:30
12:3456:78:90ab:cd:ef01:23:31
12:3456:78:90ab:cd:ef01:23:32
12:3456:78:90ab:cd:ef01:23:33
12:3456:78:90ab:cd:ef01:23:34
12:3456:78:90ab:cd:ef01:23:35
12:3456:78:90ab:cd:ef01:23:36
12:3456:78:90ab:cd:ef01:23:37

In this example, we are using the ip_network method from the ipaddress module to generate a range of all the IP addresses that represents the network.

You can find the following code in the net_planner_ipv6.py file:

#!/usr/bin/env python3

import ipaddress as ip

IPV6_ADDR = '2001:db8:0:1::'

mask = input("Enter the mask length: ")
mask = int(mask)
net_addr = IPV6_ADDR + '/' + str(mask)

print("Using network address:%s " %net_addr)
try:
network = ip.ip_network(net_addr)
except:
raise Exception("Failed to create network object")

print("This mask will give %s IP addresses" %(network.num_addresses))
print("The network configuration will be:")
print(" network address: %s" %str(network.network_address))
print(" netmask: %s" %str(network.netmask))
print(" broadcast address: %s" %str(network.broadcast_address))

Now we are going to execute the previous script with different mask lengths.

Here's an execution with a mask length of 64:

 Enter the mask length: 64
Using network address:2001:db8:0:1::/64
This mask will give 18446744073709551616 IP addresses
The network configuration will be:
network address: 2001:db8:0:1::
netmask: ffff:ffff:ffff:ffff::
broadcast address: 2001:db8:0:1:ffff:ffff:ffff:ffff

Here's an execution with a mask length of 68:

 Enter the mask length: 68
Using network address:2001:db8:0:1::/68
This mask will give 1152921504606846976 IP addresses
The network configuration will be:
network address: 2001:db8:0:1::
netmask: ffff:ffff:ffff:ffff:f000::
broadcast address: 2001:db8:0:1:fff:ffff:ffff:ffff
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset