|
|
发表于 2004-5-24 23:27:53
|
显示全部楼层
我有用php写的
# cat /root/auto-block/auto-block.php
[php]
#!/usr/bin/php
<?php
function current_time()
{
return strftime("%Y-%m-%d %H:%M:%S", time());
}
$ip_dropped = array();
function drop_ip($ip)
{
global $ip_dropped;
$ip_dropped[$ip]++;
if ($ip_dropped[$ip] > 10) {
// drop again
$cmd = "/sbin/iptables -D INPUT -s $ip -j DROP";
exec($cmd);
$ip_dropped[$ip] = 1;
} else if ($ip_dropped[$ip] > 1) {
// already dropped
return;
}
$cmd = "/sbin/iptables -A INPUT -s $ip -j DROP";
echo current_time() . " $cmd\n";
system($cmd);
}
$stdin = fopen('php://stdin', 'r');
$RUNNING_TIME = 3600;
$CALC_TIME = 1;
$LIMIT_COUNT = 20;
$LIMIT_COUNT_408 = 5;
echo "====================\nRefreshing iptables...\n--------------------\n";
system("/root/auto-block/iptables-startup.sh");
echo "--------------------\nReady..\n";
echo current_time() . "\n";
echo "--------------------\n";
$begin_time = time();
while (!feof($stdin) && (time() - $begin_time < $RUNNING_TIME)) {
$line = fgets($stdin, 256);
list ($code, $time, $ip, $host, $dummy, $firstline, $bytes) = split (" ", $line, 7);
$time_count[$time] += 1;
if (count($time_count) > $CALC_TIME) {
unset($time_count);
unset($ip_count);
unset($ip_count_code);
}
$ip_count["$ip"] += 1;
if ($ip_count["$ip"] > $LIMIT_COUNT) {
drop_ip($ip);
unset($ip_count["$ip"]);
}
$ip_count_code["$ip"]["$code"] += 1;
if ($ip_count_code["$ip"]["408"] > $LIMIT_COUNT_408) {
drop_ip($ip);
unset($ip_count_code["$ip"]);
}
}
echo "--------------------\n";
echo current_time() . "\n";
echo "--------------------\nFinished\n====================\n";
fclose($stdin);
?>
[/php] |
|