Sub topic 14
Checking your work with Python
This chapter assumes you can compute network addresses, broadcast addresses, and block sizes by hand, and that you know basic Python. Manual subnetting is error-prone, so you will use Python's standard ipaddress module as an independent check. It needs no installation.
Why verify
A wrong mask or a misaligned subnet boundary produces a plan that looks fine on paper and fails when you configure it. The ipaddress module applies the same rules you learned, so any disagreement between your answer and the module's points to a mistake in your arithmetic.
Inspecting a network
ip_network parses CIDR notation and exposes the mask, the address count, and an iterator over smaller subnets. The subnets(new_prefix=...) method splits a block into equal pieces.
import ipaddress
net = ipaddress.ip_network("192.168.10.0/24")
print(net.netmask) # 255.255.255.0
print(net.num_addresses) # 256
for sub in net.subnets(new_prefix=26):
print(sub, sub.network_address, sub.broadcast_address)
# 192.168.10.0/26 192.168.10.0 192.168.10.63
# 192.168.10.64/26 192.168.10.64 192.168.10.127
# 192.168.10.128/26 192.168.10.128 192.168.10.191
# 192.168.10.192/26 192.168.10.192 192.168.10.255Compare the output to your block-size work. A /26 has a block size of 64 in the last octet, so the subnets start at 0, 64, 128, and 192.
Repeating the bitwise method
You can also reproduce the manual method directly. This code builds the mask from the prefix length, applies AND to get the network address, and ORs the inverted mask to get the broadcast address.
import ipaddress
ip = int(ipaddress.ip_address("172.16.37.200"))
prefix = 20
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
network = ip & mask
broadcast = network | (~mask & 0xFFFFFFFF)
print(ipaddress.ip_address(mask)) # 255.255.240.0
print(ipaddress.ip_address(network)) # 172.16.32.0
print(ipaddress.ip_address(broadcast)) # 172.16.47.255The & 0xFFFFFFFF terms keep Python's unlimited-size integers within 32 bits. Without them, ~mask becomes a negative number.
Checking a VLSM plan
For VLSM, allocate the largest requirement first so every subnet starts on a correct boundary. This script computes the host bits for each need, adding 2 for the network and broadcast addresses.
import ipaddress
import math
pool = ipaddress.ip_network("192.168.1.0/24")
needs = {"A": 100, "B": 50, "C": 20, "D": 2}
next_addr = int(pool.network_address)
for name, hosts in sorted(needs.items(), key=lambda kv: -kv[1]):
host_bits = math.ceil(math.log2(hosts + 2)) # +2 for network and broadcast
net = ipaddress.ip_network((next_addr, 32 - host_bits))
print(name, net)
next_addr += net.num_addresses
# A 192.168.1.0/25
# B 192.168.1.128/26
# C 192.168.1.192/27
# D 192.168.1.224/30Each subnet begins where the previous one ends, and the sizes (128, 64, 32, 4) fit inside the 256-address pool. If a plan exceeds the pool, the final subnet would extend past the pool's range, so a careful script should also test net.subnet_of(pool).
Checking a summary route
collapse_addresses merges contiguous, aligned networks into the fewest possible prefixes.
import ipaddress
blocks = [
ipaddress.ip_network("192.168.0.0/24"),
ipaddress.ip_network("192.168.1.0/24"),
ipaddress.ip_network("192.168.2.0/24"),
ipaddress.ip_network("192.168.3.0/24"),
]
print(list(ipaddress.collapse_addresses(blocks)))
# [IPv4Network('192.168.0.0/22')]If you remove one of the four blocks, the result is no longer a single /22. That matches the rule that a summary must cover a power-of-two count of aligned blocks.
Common errors
- Passing a host address with a non-zero host part, such as
ip_network("192.168.10.5/24"), raisesValueError. Addstrict=Falseif you want the module to zero the host bits for you. - Forgetting to add 2 to the host requirement gives subnets that are one size too small.
- Using Python's
~maskwithout masking to 32 bits gives a negative broadcast calculation.
Exercise
Which call would you use to confirm that 10.1.4.0/22 is a valid summary of 10.1.4.0/24, 10.1.5.0/24, 10.1.6.0/24, and 10.1.7.0/24, and what should it print?
Show answer
Pass the four /24 networks to ipaddress.collapse_addresses and print list() of the result. It should print [IPv4Network('10.1.4.0/22')], because the four blocks are contiguous and 10.1.4.0 is aligned on a 1024-address boundary.
Exercise
Why does ipaddress.ip_network("192.168.10.77/26") raise an error, and how do you get the network 192.168.10.64/26 from it?
Show answer
The address has host bits set, since 77 is not a multiple of the /26 block size of 64. Call ipaddress.ip_network("192.168.10.77/26", strict=False), which clears the host bits and returns 192.168.10.64/26.