I’m needing to work out the IPv6 reverse zone for a given IPv6 CIDR prefix, that is a prefix with number of bits in the network on the end after a forward slash. e.g.:
- 2001:ba8:1f1:f004::/64 → 4.0.0.f.1.f.1.0.8.a.b.0.1.0.0.2.ip6.arpa
- 4:2::/32 → 22.214.171.124.126.96.36.199.ip6.arpa
- 2001:ba8:1f1:400::/56 → 0.0.4.0.1.f.1.0.8.a.b.0.1.0.0.2.ip6.arpa
I had a quick look for a module that does it, but couldn’t find one, so I hacked this subroutine together:
Is there a more elegant way? Is there a module I can replace this with?
- Arbitrary prefix length
- Use of ‘::’ anywhere legal in the address
2 thoughts on “Converting an IPv6 address to its reverse zone in Perl”
$ perl -MNet::IP -e’print Net::IP->new($ARGV)->reverse_ip’ 2001:ba8:1f1:f004::/64
Hah, brilliant, even works for ones like 2001:ba8:1f1:400::/56. Don’t know how I missed that!