ipset-goodies/combine-saves.pl
2026-04-21 04:24:00 -05:00

54 lines
1.3 KiB
Perl
Executable file

#!/usr/bin/perl -p
#
# save-combine - filter and combine output from ipset(1) save
#
# (c)2026 Corwin Brust <corwin@bru.st>
#
# You may use this program under the terms of the GNU Public License version three (3) or, at your option, any later version of that license. (GPLv3+)
#
# Use a flat-file "DB" of ipset per row in the form NAME MAX
# Drop create/add lines for any set not mentioned in the DB
# Take DB file-name from first program arg, otherwise "ipset-counts.txt"
# Input is expected on stdin in the same format as output by ipset save
#
sub max2 {
my $val = $_[0] || 1;
my $rv = 2;
$rv *= 2 until $rv > $val;
return $rv;
}
BEGIN {
my $SET_COUNTS = @ARGV ? shift : q(max-counts.txt);
open my$FH,q(<),$SET_COUNTS or die $!;
while(<$FH>) {
chomp;
my($k,$v)=split;
$m{$k}=max2( $v ) if $k and $v
}
}
if(/^create (\S+)/) {
if(exists $h{$1}) {
$_ = '';
} elsif(exists $m{$1}) {
$h{$1} = 1; # don't reissue creates
$n = $1; # grab the name
$v = $m{$n}; # lookup max
s/^create $n (.*?maxelem) \d+ (.*)$/create $n $1 $v $2/
and warn qq[set $n=$v]
}
else {
$_ = ''; # skip create when no max defined
}
}
elsif(/^add (\S+)/)
{
# $_ = '' unless exists $m{$1};
if (exists $m{$1}) {
s/$/ -exist/; # make dup-safe
} else {
$_ = '';
}
}