Question

I am trying to get the IPv6 Prefix from a given IPv6 Address given the prefix length.

I have already tried several modules (like Net::IP, Net::IPv6Addr, and Net::IPv6Address) but none seems to do exactly that.

Actually only Net::IPv6Address gives such a method but the result is not in IPv6 format.

For example: The IPv6: FE80:0202:B3FF:FE1E::/56 should give a prefix of FE80:0202:B3FF:FE00:: but when I run this code:

use Net::IPv6Address;
my $IPv6Address = new Net::IPv6Address("FE80:0202:B3FF:FE1E::", 56);
my $prefix = $IPv6Address->prefix();
print $prefix;

I get 'fe800202b3fffe' (Not the proper IPv6 format)...

I did the same in Java using the com.googlecode.ipv6.IPv6Address and com.googlecode.ipv6.IPv6NetworkMask libraries and it worked perfectly.

Any ideas on how to do this?

Thanks to everyone in advance!

Was it helpful?

Solution

There are many modules that can do this. I would suggest Net::Works or NetAddr::IP. With Net::Works:

use v5.16;

use Net::Works::Network;

my $network = Net::Works::Network->new_from_string( 
                 string => 'FE80:0202:B3FF:FE1E::/56' );
say $network->first;

This will output fe80:202:b3ff:fe00::.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top