in reply to Re: how to convert ipv4 ip to ipv6to4 ip
in thread how to convert ipv4 ip to ipv6to4 ip
Nice work. I've just had cause to do the reverse so here is the sub I wrote in case it is useful to someone else.
use strict; use warnings; use Regexp::Common 'net'; use Carp; sub to_ipv4 { my $v6 = shift; unless ($v6 =~ /$RE{net}{IPv6}{-keep}/) { carp "'$v6' is not a valid IPv6 address"; return; } unless ($2 eq '2002') { carp "'$v6' is not a 6to4 IPv6 address"; return; }; my @octets; my @parts = ($4, $3); for my $part (@parts) { unshift @octets, hex substr ($part, -2, 2, '') for 1, 2; } return join '.', @octets; } # Example usage my $v4 = to_ipv4 ($ARGV[0]) // 'invalid'; print "$v4\n";
🦛
|
|---|