|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +=head1 NAME |
| 4 | +
|
| 5 | +expand-ipv6 - Expand IPv6 addresses to their full width. |
| 6 | +
|
| 7 | +=head1 SYNOPSIS |
| 8 | +
|
| 9 | + expand-ipv6 [options] address1 .. addressN |
| 10 | +
|
| 11 | + Command-Line Options: |
| 12 | +
|
| 13 | + --bare Don't show the colons in the output. |
| 14 | + --help Show the help-information for this script. |
| 15 | +
|
| 16 | +=cut |
| 17 | + |
| 18 | +=head1 DESCRIPTION |
| 19 | +
|
| 20 | +This script will expand each specified IPv6 address into the full-form, |
| 21 | +this is useful if you're trying to setup DNS-entries, etc. |
| 22 | +
|
| 23 | +=cut |
| 24 | + |
| 25 | +=head1 AUTHOR |
| 26 | +
|
| 27 | + Steve |
| 28 | + -- |
| 29 | + http://www.steve.org.uk/ |
| 30 | +
|
| 31 | +=cut |
| 32 | + |
| 33 | +=head1 LICENSE |
| 34 | +
|
| 35 | +Copyright (c) 2016 by Steve Kemp. All rights reserved. |
| 36 | +
|
| 37 | +This script is free software; you can redistribute it and/or modify it under |
| 38 | +the same terms as Perl itself. |
| 39 | +
|
| 40 | +The LICENSE file contains the full text of the license. |
| 41 | +
|
| 42 | +=cut |
| 43 | + |
| 44 | + |
| 45 | +use strict; |
| 46 | +use warnings; |
| 47 | + |
| 48 | +use Getopt::Long; |
| 49 | +use Pod::Usage; |
| 50 | + |
| 51 | + |
| 52 | +# |
| 53 | +# Parse any command-line options which are present. |
| 54 | +# |
| 55 | +my %config = parsedOptions(); |
| 56 | + |
| 57 | +# |
| 58 | +# Count of addreseses we've expanded |
| 59 | +# |
| 60 | +my $count = 0; |
| 61 | + |
| 62 | +# |
| 63 | +# Process every argument. |
| 64 | +# |
| 65 | +while ( my $addr = shift ) |
| 66 | +{ |
| 67 | + $count += 1; |
| 68 | + |
| 69 | + # Expand the IP-address |
| 70 | + my $expanded = expand($addr); |
| 71 | + |
| 72 | + # If we're not supposed to show the colons then remove them. |
| 73 | + if ( $config{ 'bare' } ) |
| 74 | + { |
| 75 | + $expanded =~ s/://g; |
| 76 | + } |
| 77 | + |
| 78 | + # Show the necessary. |
| 79 | + print $expanded . "\n"; |
| 80 | +} |
| 81 | + |
| 82 | +# |
| 83 | +# Exit cleanly if we received an argument. |
| 84 | +# |
| 85 | +exit( $count > 0 ? 0 : 1 ); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +=begin doc |
| 91 | +
|
| 92 | +Expand the specified IPv6 address. |
| 93 | +
|
| 94 | +=end doc |
| 95 | +
|
| 96 | +=cut |
| 97 | + |
| 98 | +sub expand |
| 99 | +{ |
| 100 | + my ($arg) = (@_); |
| 101 | + |
| 102 | + # Sanity-check our argument. |
| 103 | + die "Missing address" unless $arg; |
| 104 | + die "Invalid address" unless ( $arg =~ /^([a-f0-9:]+)$/i ); |
| 105 | + |
| 106 | + # Temporary value |
| 107 | + my @tmp; |
| 108 | + |
| 109 | + # Parts of the address we were given |
| 110 | + my @parts = split( ':', $arg ); |
| 111 | + |
| 112 | + # Process each part. |
| 113 | + for my $part (@parts) |
| 114 | + { |
| 115 | + if ( length($part) ) |
| 116 | + { |
| 117 | + push( @tmp, substr( "0000$part", -4 ) ); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + for my $i ( 1 .. ( 8 - int(@parts) ) ) |
| 122 | + { |
| 123 | + push( @tmp, "0000" ); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return join( ":", @tmp ); |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +=begin doc |
| 134 | +
|
| 135 | +Parse any specified options and return suitable values. |
| 136 | +
|
| 137 | +=end doc |
| 138 | +
|
| 139 | +=cut |
| 140 | + |
| 141 | +sub parsedOptions |
| 142 | +{ |
| 143 | + my %vars; |
| 144 | + |
| 145 | + # |
| 146 | + # Defaults - Bare here means without the colons. Useful for DNS |
| 147 | + # if you're using TinyDNS, dns-api.com, or similar service. |
| 148 | + # |
| 149 | + $vars{ 'bare' } = 0; |
| 150 | + |
| 151 | + exit |
| 152 | + if ( |
| 153 | + !GetOptions( "help" => \$vars{ 'help' }, |
| 154 | + "bare" => \$vars{ 'bare' }, ) ); |
| 155 | + |
| 156 | + pod2usage(1) if ( $vars{ 'help' } ); |
| 157 | + |
| 158 | + return (%vars); |
| 159 | + |
| 160 | +} |
0 commit comments