#!/usr/bin/perl
my %counts = get_ips();
foreach my $ip (keys %counts) {
if ($counts{$ip} > 5) {
$check = rindex $ip,"*.*..";
if ($check eq "-1"){
$blocked = `ipfw -q add 200 deny all from $ip to any in via fxp0`;
open(FILE, ">>path"); #path of file leave the '>>' intact example: ">>etc/log.txt"
print FILE "Blocked " . $ip . "\n";
close FILE;
}
}
}
sub get_ips {
my %counts;
foreach my $line (`netstat -n -A ip`) {
if ($line =~ /ESTABLISHED/) {
my @octets = split(/\./, (split(/\s+/, $line))[4]);
# the first four are the ip..
my $ip = join('.', @octets[0..3]);
$counts{$ip}++;
}
}
return %counts;
}