Sub topic 17
Inspecting addresses in practice
This chapter assumes you can read IPv6 notation, know the main prefixes (2000::/3, fe80::/10, fc00::/7, ff00::/8), and have used ip or ping on IPv4. It shows how to check what a host has and how to classify an address in code.
Looking at a host
On Linux, three commands cover most checks.
ip -6 addr show
ip -6 route show
ping -6 -c 3 ::1ip -6 addr show lists each interface's IPv6 addresses with a scope field. Expect scope link for the fe80:: address, scope global for global unicast or unique local addresses, and scope host for ::1. Flags such as temporary mark privacy addresses, and dynamic marks addresses learned through autoconfiguration. ip -6 route show shows the on-link prefixes and the default route learned from a Router Advertisement. ping -6 ::1 tests only the local IPv6 stack, not the network.
Classifying addresses in Python
The ipaddress module converts between forms and classifies by prefix.
import ipaddress
addr = ipaddress.ip_address("2001:0db8:0000:0000:0000:ff00:0042:8329")
print(addr.compressed) # 2001:db8::ff00:42:8329
print(addr.exploded) # 2001:0db8:0000:0000:0000:ff00:0042:8329Use exploded when you need to compare or sort text by position. Use compressed for display.
import ipaddress
for text in ["::1", "fe80::1", "fd12:3456:789a::1", "ff02::1"]:
a = ipaddress.ip_address(text)
print(text, a.is_loopback, a.is_link_local, a.is_private, a.is_multicast)
# ::1 True False True False
# fe80::1 False True True False
# fd12:3456:789a::1 False False True False
# ff02::1 False False False TrueChecking a subnet plan
The module can also verify a plan. A /48 leaves 16 bits for the subnet ID.
import ipaddress
from itertools import islice
site = ipaddress.ip_network("2001:db8:abcd::/48")
print(2 ** (64 - site.prefixlen)) # 65536 possible /64 subnets
for subnet in islice(site.subnets(new_prefix=64), 3):
print(subnet)
# 2001:db8:abcd::/64
# 2001:db8:abcd:1::/64
# 2001:db8:abcd:2::/64Use islice because subnets() yields lazily. Calling list() on all 65,536 results works but wastes memory.
Reproducing an EUI-64 address
Older devices may build the interface identifier from the MAC address. This function repeats the method: it flips the universal/local bit and inserts ff:fe in the middle.
import ipaddress
def eui64_link_local(mac):
b = bytearray(int(x, 16) for x in mac.split(":"))
b[0] ^= 0x02 # flip the universal/local bit
iid = bytes(b[:3]) + b"\xff\xfe" + bytes(b[3:])
return ipaddress.IPv6Address(b"\xfe\x80" + bytes(6) + iid)
print(eui64_link_local("00:1a:2b:3c:4d:5e")) # fe80::21a:2bff:fe3c:4d5eIf an address on your host matches this pattern, it exposes the MAC address. Privacy and stable opaque identifiers avoid that.
Common errors
- Pinging a link-local address without naming the interface. Use
ping -6 fe80::1%eth0, because the same fe80:: address can exist on every link. - Comparing addresses as strings.
2001:db8::1and2001:0db8:0:0:0:0:0:1are the same address. Parse both withipaddressfirst. - Reading a successful
::1ping as proof of connectivity.
Exercise
A host shows the address fd12:3456:789a::1 and is_private returns True. Why is that not enough to call it a unique local address, and what check is correct?
Show answer
is_private is also True for loopback and link-local addresses, so it does not identify a specific type. Test whether the address is inside fc00::/7, for example ipaddress.ip_address("fd12:3456:789a::1") in ipaddress.ip_network("fc00::/7").