Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: how to convert ipv4 ip to ipv6to4 ip or the reverse

by hippo (Bishop)
on Feb 24, 2021 at 10:18 UTC ( [id://11128733]=note: print w/replies, xml ) Need Help??


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

🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11128733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found