in reply to how to convert ipv4 ip to ipv6to4 ip

Do you want to solve 6to4 with perl?

# take IP4 address string my $IP_str = '192.0.2.4'; # convert the numbers to hex my @IP4_a = map { sprintf("%02x", $_) } split(/\./,$IP_str); # embed these hex numbers in the IPv6 template my $R = sprintf("2002:%s%s:%s%s::/48", @IP4_a); # output the result die "($R)";

something like that?

sub ipv4_6to4{ return sprintf("2002:%s%s:%s%s::/48", map {sprintf("%02x",$_)} split +(/\./,$_[0])); }

Replies are listed 'Best First'.
Re^2: how to convert ipv4 ip to ipv6to4 ip or the reverse
by hippo (Archbishop) on Feb 24, 2021 at 10:18 UTC

    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";

    🦛

Re^2: how to convert ipv4 ip to ipv6to4 ip
by Anonymous Monk on Dec 28, 2016 at 08:33 UTC
    sub ipv4_6to4 works like a charm. Thanks a ton. :)