Thank you gellyfish. I was reading /dev/stdin instead of STDIN because that part is pure cargo from my inspiration at
ApacheLogsWithoutIPs. After fixing that and learning from a horde of zombies to call "exit" instead of "next" from inside the child to continue looping in the parent, it seems to function as intended and very efficiently.
#!/usr/bin/perl -w
$SIG{CHLD} = 'IGNORE';
use strict;
use Fcntl qw(:DEFAULT);
use Net::Netmask;
use Net::Whois::IANA;
use Proc::Fork;
use SDBM_File;
my $iana = Net::Whois::IANA->new;
my $bye = join '|', (
'strings known to occur',
'in custname and orgname',
);
my $badhosts = 'some\.bad|other\.worse\d+';
tie (my %seen, 'SDBM_File', '/some/sdbm', O_RDWR|O_CREAT, 0666)
or die "$!";
while (<STDIN>) {
chomp;
next if exists $seen{$_};
$seen{$_} = 1;
my $ip = $_;
child {
my $problem = 0;
my $host = `host $ip` or die "$!";
$problem = 1 if $host =~ /$badhosts/i;
if ($problem or $host =~ /not found/) {
$iana->whois_query(-ip=>$ip);
my $inetnum = $iana->inetnum || '';
my $custname = $iana->{QUERY}->{custname} || '';
my $orgname = $iana->{QUERY}->{orgname} || '';
exit unless $inetnum
and ($problem or $custname or $orgname);
if ($problem or
$custname =~ /$bye/i or $orgname =~ /$bye/i){
my $mask = Net::Netmask->new($inetnum);
`iptables -A INPUT -s $mask -j DROP`;
}
}
exit
};
}
untie %seen;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.