-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidr2ips
executable file
·69 lines (47 loc) · 1.57 KB
/
cidr2ips
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
#!/usr/bin/perl
=head1 NAME
cidr2ips - Convert CIDRs (or even IPs) to a list of IPs
=head1 SYNOPSIS
Arguments are assumed to be files. No options are currently supported. To
convert CIDRs as a quick one-liner, pipe this from echo, e.g.
echo 1.2.3.4/18 5.6.7.8/27 |cidr2ips
or
cidr2ips <<< "1.2.3.4/14 127/8"
You can also pipe this from an SPF record, e.g.
host -t TXT github.com |cidr2ips
This script requires perl module Net::CIDR.
Part of net-scripts: https://github.com/adamhotep/net-scripts
cidr2ips 0.3.20120815.1 Copyright 2010+ by Adam Katz, GPLv2+
=head1 AUTHORS
Adam Katz E<lt>https://github.com/adamhotepE<gt>
=cut
use strict;
use warnings;
if ( $ARGV[0] && $ARGV[0] =~ /^--?h/ ) {
use Pod::Usage "pod2usage";
pod2usage({-verbose=>2, -exitval=>0, -output=>\*STDOUT});
}
use Net::CIDR "cidr2octets";
my @cidrs = ();
my $octet = '(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)';
while(<>) {
while(/.*?(?!<[.\/])\b($octet(?:(?:\.$octet){3}|(?:\.$octet){0,3}(?=\/[0-3]?\d\b)))(\/[0-3]?\d)?\b(?![.\/])/g) {
push (@cidrs, $1 . ($2 || "/32")); # support bare IPs
}
}
exit 1 if not @cidrs;
foreach ( Net::CIDR::cidr2octets(@cidrs) ) {
# cidr2octets will truncate, e.g. 10.0.0.0/8 => 10, so we have to un-truncate
/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$/;
my $a = $1;
my @second = (defined $2 ne "" ? ($2) : (0..255));
my @third = (defined $3 ne "" ? ($3) : (0..255));
my @fourth = (defined $4 ne "" ? ($4) : (0..255));
foreach my $b (@second) {
foreach my $c (@third) {
foreach my $d (@fourth) {
print "$a.$b.$c.$d\n";
}
}
}
}