Converting an IPv6 address to its reverse zone in Perl

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 → 2.0.0.0.4.0.0.0.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?

Must support:

  • Arbitrary prefix length
  • Use of ‘::’ anywhere legal in the address

2 thoughts on “Converting an IPv6 address to its reverse zone in Perl

  1. $ perl -MNet::IP -e’print Net::IP->new($ARGV[0])->reverse_ip’ 2001:ba8:1f1:f004::/64
    4.0.0.f.1.f.1.0.8.a.b.0.1.0.0.2.ip6.arpa.

Leave a Reply

Your email address will not be published. Required fields are marked *