From fe64bf8a39d11f1768fd2bf380c42cfee1edee37 Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Wed, 23 Mar 2016 10:24:18 +0100 Subject: [PATCH] Initial commit --- ipv6expand.pl | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 ipv6expand.pl diff --git a/ipv6expand.pl b/ipv6expand.pl new file mode 100755 index 0000000..16af512 --- /dev/null +++ b/ipv6expand.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +sub count_groups { # See hown many groups we have + die "Too many arguments for subroutine" if @_ < 1; + die "Too few arguments for subroutine" if @_ > 1; + my $address = shift; + + # Make sure we have just enough double colons and count them if it is all right + my $colon_no = () = $address =~ /::/g; + die "Invalid ipv6 address" if $colon_no > 1; + my $no = 1; + $no += () = $address =~ /:/g; + return $no; + +} + +# If we have one occurence of :: replace with correct amount of zeros and colons +sub expand_void { + die "Too many arguments for subroutine" if @_ < 1; + die "Too few arguments for subroutine" if @_ > 1; + my $address = shift; + my $no_colons = 8 - count_groups($address); + my $insert = "0000:0000"; + for (my $i = 0; $i < $no_colons - 1; $i++) { + $insert = $insert . ":0000" + } + $address =~ s/::/:$insert:/g; + return $address; + +} + +# Fix a single group and padd with zeros +sub fix_group { + die "Too many arguments for subroutine" if @_ < 1; + die "Too few arguments for subroutine" if @_ > 1; + my $group = shift; + my $len = length($group); + die "Group has too many digits" if $len > 4; + while ($len < 4) { + $group = "0$group"; + $len++; + + } + # Make sure it is a valid hexnumber + die "Group is not a hex number" if $group !~ /^[0-9a-fA-F]+$/; + return $group; + +} + +# Loop all groups and fix them with fix_group +sub fix_groups { + die "Too many arguments for subroutine" if @_ < 1; + die "Too few arguments for subroutine" if @_ > 1; + my $a = shift; + my @groups = split(/:/, $a); + my $address = ""; + foreach my $group (@groups) { + $group = fix_group($group); + $address = "$address$group:"; + } + + $address =~ s/:+$//; + return $address; +} + +# Now put it together +# Loop through stdin +while (<>) { + chomp; # Remove newline + my $addr = $_; # Store address + + if (count_groups($addr) != 8) { + $addr = expand_void($addr); + } + + $addr = fix_groups($addr); + + # Double check that we have enough groups one last time + die "Not a valid ipv6 address" if count_groups($addr) != 8; + + # Lower case is preffered for ipv6 adresses + print lc "$addr\n"; + +} + +exit 0;