Worked example: subnetting any prefix without a cheat sheet
Audit four interface addresses (/21, /29, /31 and /32) by hand with a four-step method that touches only one octet, catch a broadcast address assigned to a host, turn a range back into prefixes, and confirm every answer with Python ipaddress.
Contentsand lesson path
On this page
- The problem: audit four interfaces with no subnet chart
- Step 1: The four-step method, and why each step works
- Step 2: Work the four lines, including /31 and /32
- Step 3: Confirm every answer with Python ipaddress
- Check your understanding
- Apply it
- Review prompt
Lessons of IPv4 Addressing & Subnetting
- Addresses as 32-bit numbers: binary, masks and prefixes Concept · 20 min
- Worked example: subnetting any prefix without a cheat sheet Worked example · 25 min
- VLSM planning: sizing, ordering and leaving room to summarize Worked example · 25 min
- Special-purpose ranges: private, CGNAT, loopback, link-local and documentation Concept · 15 min
- Lab: on-link vs off-link delivery - ARP, the gateway and ICMP in a capture Lab · 25 min
- Check: timed subnetting and addressing-plan exercise Knowledge check · 8 questions
What you will learn
- Subnet any IPv4 prefix by hand with binary and mask arithmetic, including /31 point-to-point links and /32 host routes, and check the result with a script.
Prerequisites: Networking Basics
01. The problem: audit four interfaces with no subnet chart
Suppose you are reviewing a change request before a maintenance window. It touches four interface lines taken from router and server configurations:
172.20.77.130/21on a server VLAN interface;203.0.113.207/29on a load balancer;10.44.0.14/31on a router-to-router link;10.44.255.3/32on a router loopback.
For each line you need the mask, the network address, the broadcast address where one exists, the usable host range, and one more answer that reviews often skip: is the configured address actually a usable host address? The constraints are those of a real review. There is no subnet chart in the room, you want to work by hand so that you can explain each answer, and you must still confirm everything with a script before the change is approved. Lesson 1 gave you the model: a mask is a run of 1 bits, the network is the address ANDed with the mask, and every block starts at a multiple of its size. This lesson turns that model into a method that touches only one octet.
02. Step 1: The four-step method, and why each step works
Converting all 32 bits to binary every time is slow and error-prone. Only one octet contains the boundary between network and host bits. Every octet to its left is all network bits and stays as it is; every octet to its right is all host bits. So the method works on that interesting octet alone:
- Find the interesting octet. Write the prefix as whole octets plus leftover bits.
/21is8 + 8 + 5, so the boundary sits in octet 3, with 5 network bits and 3 host bits there. - Find the block size in that octet: 2 raised to the number of host bits in it. For
/21that is2^3 = 8. The host bits inside the octet count from 0 to 7, so blocks repeat every 8 values. - Find the mask octet:
256 - block size. For/21that is256 - 8 = 248, so the mask is255.255.248.0. This works because 248 in binary is11111000: subtracting a power of two from 256 leaves exactly the 1 bits above it. - Round down, then fill. The network value is the largest multiple of the block size that is not bigger than the octet value; every octet to the right becomes 0. The broadcast value is that multiple plus
block size - 1; every octet to the right becomes 255. Rounding down to a multiple of 8 is the same as ANDing with 248, which is why the method gives the same answer as lesson 1's bitwise AND.
03. Step 2: Work the four lines, including /31 and /32
Line A, 172.20.77.130/21. Interesting octet 3, block 8, mask 255.255.248.0. The multiples of 8 around 77 are 72 and 80, so the network is 172.20.72.0 and the broadcast is 172.20.79.255. The block holds 2^11 = 2,048 addresses, so 2,046 are usable, from 172.20.72.1 to 172.20.79.254. The configured .77.130 sits inside that range: it is a valid host.
Line B, 203.0.113.207/29. /29 is 8 + 8 + 8 + 5, so the interesting octet is octet 4, with 3 host bits: block 8, mask 255.255.255.248. The largest multiple of 8 not above 207 is 200, so the network is 203.0.113.200, the broadcast is 203.0.113.207, and the usable range is .201 to .206, six hosts. This is the audit finding: the load balancer has been given the block's broadcast address. Linux accepts that configuration without complaint, but every neighbour on the /29 has installed .207 as the broadcast address for that subnet, so traffic they send to it goes to all hosts, and Linux's own ping refuses it with Do you want to ping broadcast? Then -b. The fix is to move the load balancer to one of .201 to .206.
Line C, 10.44.0.14/31. The interesting octet is octet 4 with one host bit: block 2, mask 255.255.255.254. 14 is even, so the pair is 10.44.0.14 and 10.44.0.15. On a point-to-point link, RFC 3021 makes both of them host addresses, one for each router. What if someone "corrects" this link to a /30? The block size becomes 4, and 14 rounds down to 12: the link becomes 10.44.0.12/30, with .12 as the network, .15 as the broadcast and .13 and .14 as the usable pair. Our end still works, but the far router's .15 is now a broadcast address, so the change silently breaks the link unless both ends are renumbered together.
Line D, 10.44.255.3/32. No host bits, block 1, mask 255.255.255.255. The network is the address itself and there is no broadcast. Loopbacks get /32s because they belong to the router rather than to any link; routing protocols then advertise them as host routes.
What if Line A had a different prefix? Each bit moves the boundary by a factor of two. At /20 the block size in octet 3 is 16, so 77 rounds down to 64: 172.20.64.0/20, broadcast 172.20.79.255. At /22 the block size is 4, so 77 rounds down to 76: 172.20.76.0/22, broadcast again 172.20.79.255. At /23 the block size is 2: 172.20.76.0/23, broadcast 172.20.77.255. The network start moves with every change, which is easy to miss when someone edits a prefix length without recomputing.
Going the other way, from a range to prefixes. Firewall objects and route filters often start from a range rather than a prefix. One prefix can cover a range only if the range holds a power-of-two number of addresses and starts at a multiple of that number. 172.20.77.0 to 172.20.78.255 holds 512 addresses, the size of a /23, but a /23 must start on an even third octet and 77 is odd. So the range needs two prefixes, 172.20.77.0/24 and 172.20.78.0/24. Python's summarize_address_range() performs this split.
When not to rely on mental arithmetic alone. Hand calculation is for understanding, whiteboard reviews and quick triage. For anything that goes into a change request, an infrastructure-as-code variable or an address management record, a script is cheaper than a mistake: it does not get tired at 3 a.m., and it catches findings like Line B.
04. Step 3: Confirm every answer with Python ipaddress
Python's standard ipaddress module has two constructors for this job, and choosing the right one matters. ip_interface("172.20.77.130/21") accepts a host address with its prefix length, exactly what an interface line contains, and .network gives the block it lives in. ip_network("172.20.77.130/21") instead expects a network address. By default it runs with strict=True and raises ValueError when any host bit is set, which is useful: a network declaration with host bits set is usually a typo. Use ip_interface for interface lines and ip_network for network declarations. hosts() already handles both exceptions: its documentation says a /31 returns both addresses and a /32 returns its single address.
Save the script as audit.py and run it with python3 audit.py. It needs Python 3 and nothing else.
import ipaddress
configs = ["172.20.77.130/21", "203.0.113.207/29", "10.44.0.14/31", "10.44.255.3/32"]
for text in configs:
iface = ipaddress.ip_interface(text) # a host address plus its prefix length
net = iface.network # the block that address lives in
hosts = list(net.hosts()) # usable addresses, RFC 3021 aware
print(f"{text:17} net={str(net):17} mask={net.netmask}")
print(f"{'':17} hosts={len(hosts)} {hosts[0]}..{hosts[-1]} "
f"configured-is-host={iface.ip in hosts}")
first = ipaddress.ip_address("172.20.77.0")
last = ipaddress.ip_address("172.20.78.255")
blocks = ipaddress.summarize_address_range(first, last)
print("range 172.20.77.0-172.20.78.255 ->", ", ".join(str(b) for b in blocks))
try:
ipaddress.ip_network("172.20.77.130/21") # strict=True by default
except ValueError as err:
print("ip_network error:", err)172.20.77.130/21 net=172.20.72.0/21 mask=255.255.248.0
hosts=2046 172.20.72.1..172.20.79.254 configured-is-host=True
203.0.113.207/29 net=203.0.113.200/29 mask=255.255.255.248
hosts=6 203.0.113.201..203.0.113.206 configured-is-host=False
10.44.0.14/31 net=10.44.0.14/31 mask=255.255.255.254
hosts=2 10.44.0.14..10.44.0.15 configured-is-host=True
10.44.255.3/32 net=10.44.255.3/32 mask=255.255.255.255
hosts=1 10.44.255.3..10.44.255.3 configured-is-host=True
range 172.20.77.0-172.20.78.255 -> 172.20.77.0/24, 172.20.78.0/24
ip_network error: 172.20.77.130/21 has host bits setEvery hand answer matches, and the script flags Line B with configured-is-host=False. The last line shows the strict check at work on an interface line. If you are tempted to silence it with strict=False, be careful: Python will then quietly mask off the host bits and return 172.20.72.0/21, so a typo in a network declaration becomes a different, valid-looking network instead of an error.
05. Check your understanding
06. Apply it
Take three interface lines from a lab or real configuration, or use 172.31.200.9/19, 198.51.100.140/26 and 10.9.0.254/31. Work each one by hand with the four steps, including whether the configured address is a usable host, then add them to audit.py and compare. Finally, extend the script so that it prints a clear warning for any line whose address is the network or broadcast address of its block; this check belongs in the address management table that the skill's practice task asks you to build.
Next, VLSM planning combines blocks of different sizes into one plan for a site.
07. Review prompt
From memory, state the four steps and explain in binary why each one works. Then work 10.70.130.77/18 without notes (mask, network, broadcast and usable count), and explain the difference between ip_interface(x).network and ip_network(x) when a script validates configuration input.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.