All pastes #596252 Raw Edit

Miscellany

public text v1 · immutable
#596252 ·published 2007-06-29 21:01 UTC
rendered paste body
#!/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;
}