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