-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidr2regex
executable file
·67 lines (45 loc) · 1.53 KB
/
cidr2regex
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
#!/usr/bin/perl
=head1 NAME
cidr2regex - Convert IPs, with or without CIDRs, to a single Regular Expression
=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 |cidr2regex
or
cidr2regex <<< "1.2.3.4/14 127/8"
This script requires perl modules Regexp::Assemble and Net::CIDR.
Part of net-scripts: https://github.com/adamhotep/net-scripts
cidr2regex 0.3.20180708.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});
exit;
}
use Regexp::Assemble;
use Net::CIDR "cidr2octets";
my @cidrs = ();
my $re = Regexp::Assemble->new(anchor_word => 1);
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 or "/32")); # support bare IPs
}
}
exit 1 if not @cidrs;
$octet = ".$octet";
foreach ( Net::CIDR::cidr2octets(@cidrs) ) {
# cidr2octets will truncate, e.g. 10.0.0.0/8 => 10, so we have to un-truncate
my ($a, $b, $c, $d) = /^(\d+)(\.\d+)?(\.\d+)?(\.\d+)?$/;
next unless $a;
$b ||= $octet;
$c ||= $octet;
$d ||= $octet;
$re->add( sprintf('%s\%s\%s\%s', $a, $b, $c, $d) );
}
#$re =~ s/^\(\?-xism:(.*)\)$/(?:$1)/;
print $re->as_string. "\n";