Skip to content

Instantly share code, notes, and snippets.

@sebastianw
Created October 19, 2021 11:23
Show Gist options
  • Save sebastianw/52462a99e2d3a604751af705d0d27160 to your computer and use it in GitHub Desktop.
Save sebastianw/52462a99e2d3a604751af705d0d27160 to your computer and use it in GitHub Desktop.
IPv4 DNS Reverse Zone generation
#! /usr/bin/env python3
import ipaddress
subnet = ipaddress.IPv4Network("192.168.0.0/22", strict=False)
if subnet.prefixlen > 24:
raise ValueError("No Zone for subnets >/24")
elif subnet.prefixlen > 16:
zone_length = 24
split_length = 1
elif subnet.prefixlen > 8:
zone_length = 16
split_length = 2
else:
zone_length = 8
split_length = 3
prefix_diff = zone_length - subnet.prefixlen
if prefix_diff > 0:
zone_subnets = subnet.subnets(zone_length - subnet.prefixlen)
else:
zone_subnets = [subnet]
for subzone in zone_subnets:
zone = list(reversed(str(subzone.network_address).split(".")))
zone = ".".join(zone[split_length:]) + ".in-addr.arpa"
print(zone)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment