forked from skx/sysadmin-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidr2ip
executable file
·71 lines (47 loc) · 1 KB
/
cidr2ip
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
#!/usr/bin/env perl
=head1 NAME
cidr2ip - Convert CIDR notation to distinct IPs.
=head1 SYNOPSIS
cidr2ip 192.168.0.0/24 ..
=cut
=head1 DESCRIPTION
This script will expand each specified CIDR range into a collection of
distinct IP addresses.
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
=cut
=head1 LICENSE
Copyright (c) 2013 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;
sub bin2dd
{
join '.', unpack 'C4', pack 'N', $_[0];
}
sub dd2bin
{
unpack 'N', pack 'C4', split '\.', $_[0];
}
sub CIDRList
{
my $iter = 0xffffffff >> $_[0];
my $mask = ~$iter;
my $lo = dd2bin( $_[1] ) & $mask;
map {bin2dd $lo++} 0 .. $iter;
}
while ( my $cidr = shift )
{
if ( $cidr =~ /^(.*)\/(.*)$/ )
{
my $i = $1;
my $m = $2;
print "$_\n" for CIDRList $m, $i;
}
}