This commit is contained in:
corwin 2026-04-21 04:24:00 -05:00
parent 8856d187ba
commit c53c1299eb
6 changed files with 159 additions and 0 deletions

16
combine-counts.pl Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/perl -nl
# print the name and total number of records in each unique ipset
#(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+)
#
# Input is expected on stdin as NAME COUNT and may contain duplicates NAMES
#
END {
print qq($_ $h{$_} )
for sort keys %h
}
$h{$1} += $2
if /^(\S+) (\d+)$/

54
combine-saves.pl Executable file
View file

@ -0,0 +1,54 @@
#!/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 {
$_ = '';
}
}

4
destroy-all.sh Executable file
View file

@ -0,0 +1,4 @@
#!/usr/bin/bash
for s in $( ipset -n list ) ; do ipset destroy $s ; done

36
save-add-deduplicate.pl Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/perl -p
# combine ipset save files setting max per a flat file of ipset per row in the form NAME MAX
#
#(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+)
#
# Input is expected on stdin in the same format as output by ipset save
#
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}=$v if $k and $v
}
}
if(/^create (\S+)/) {
if(exists $h{$1}) {
$_ = '';
} elsif(exists $m{$1}) {
$h{$1} = $1;
$n = $m{$1};
s/^(create (\S+).*?hashsize) \d+ (.*)$/$1 $n $3/
}
else {
$_ = ''; # skip create when no max defined
}
}
elsif(/^add (\S+)/)
{
$_ = '' unless exists $m{$1};
}

41
save-add-deduplicate.sh Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/perl -p
# combine ipset save files setting max per a flat file of ipset per row in the form NAME MAX
#
#(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+)
#
# Input is expected on stdin in the same format as output by ipset save
#
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}=$v if $k and $v
}
}
if(/^create (\S+)/) {
if(exists $h{$1}) {
$_ = '';
} elsif(exists $m{$1}) {
$h{$1} = $1;
$n = $m{$1};
s/^(create (\S+).*?hashsize) \d+ (.*)$/$1 $n $3/
}
else {
$_ = ''; # skip create when no max defined
}
}
elsif(/^add (\S+)/)
{
$_ = '' unless exists $m{$1};
}

8
save-count.pl Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/perl -nl
# print the name and number of records in each non-empty ipset
#(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+)
sub p{ print qq($n $c) if $n and $c }
if ( /^create (\S+)/ ) { p(); $c=0; $n=$1 } else { ++$c if $n and /^add/ }
END{ p() }