-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone-comment.pl
executable file
·67 lines (54 loc) · 1.42 KB
/
zone-comment.pl
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
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
# ./zone-comment.pl /etc/bind/hosts.db ~dpavlin/ips/ips.free > /tmp/zone.comment
# ./zone-comment.pl /etc/bind/hosts.rev212 /tmp/zone.extra.ptr > /tmp/hosts.rev212
my ( $zone, $ips ) = @ARGV;
die "Usage: $0 zone ips\n" unless $zone && $ips;
my $debug = $ENV{DEBUG} || 0;
my $ip_regex;
open(my $fh, '<', $ips);
my ( $prefix, $suffix );
my @ips = map {
chomp;
my $line = $_;
$prefix = $line if ! defined $prefix;
$suffix = $line if ! defined $suffix;
if ( $line !~ m/^\Q$prefix\E/ ) {
my $l = length($prefix);
foreach my $j ( 0 .. $l ) {
my $i = $l - $j;
if ( substr($line,0,$i) eq substr($prefix,0,$i) ) {
$prefix = substr($prefix,0,$i);
last;
}
}
}
if ( $line !~ m/\Q$suffix\E$/ ) {
my $l = length($suffix);
foreach my $j ( 0 .. $l ) {
my $i = $l - $j;
if ( substr($line,-$i,$i) eq substr($suffix,-$i,$i) ) {
$suffix = substr($suffix,-$i,$i);
last;
}
}
}
#warn "XXX [$prefix] $line [$suffix]\n";
$line;
} <$fh>;
warn "# prefix [$prefix]\n" if $debug;
warn "# suffix [$suffix]\n" if $debug;
$ip_regex = join('|', map { chomp; s/^\Q$prefix\E//; s/\Q$suffix\E$//; $_; } @ips);
$ip_regex = '\b' . $prefix . '(' . $ip_regex . ')' . $suffix;
$ip_regex =~ s/\./\\./g; # quote dots
warn "# ip_regex $ip_regex" if $debug;
open(my $z_in, '<', $zone);
while(<$z_in>) {
if ( m/$ip_regex/ ) {
print ';XXX ', $_;
} else {
print $_;
}
}