Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to convert ipv4 ip to ipv6to4 ip

by Anonymous Monk
on Dec 27, 2016 at 13:44 UTC ( [id://1178527]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how to convert ipv4 ip to ipv6to4 ip using any perl package?
I have tried with NetAddr::IP::Util but it didn't work for me. So please help me.

Replies are listed 'Best First'.
Re: how to convert ipv4 ip to ipv6to4 ip
by FreeBeerReekingMonk (Deacon) on Dec 27, 2016 at 22:43 UTC
    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])); }

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

      🦛

      sub ipv4_6to4 works like a charm. Thanks a ton. :)
Re: how to convet ipv4 ip to ipv6to4 ip
by Corion (Patriarch) on Dec 27, 2016 at 13:48 UTC

    What problem are you trying to solve?

    Are you trying to add IPv6 connectivity to an application that only speaks IPv4? Are you trying to add IPv4 connectivity to an application that only speaks IPv6?

    Are you trying to find the IPv4 address of a user that connects to your application using IPv6? Are you trying to find the IPv6 address of a user that connects to your application via IPv4?

    If you can show us a short, self-contained example (in Perl) of what you have and what you need, then we can likely give you much better advice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-25 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found