根据HAPROXY日志中的每小时连接数大于设定阀值的IP封闭
# cat blockip.sh
#/bin/bash
#定义过滤时间
ntime=`date +%H`:00:00
otime=`date +%H --date="-1hour"`:00:00
#将定义起止时间内的日志滤出来并按连接数阀值降序,最后将IP地址写入临时文件tempa
cat /var/log/haproxy/haproxy.log|awk {'if($3>'\"$otime\"' && $3<'\"$ntime\"') print$6'}|cut -d: -f 1|sort|uniq -c|sort -rnk1|head -200|awk '{if ($1>1000) print}'>tempa
#排除白名单文件whlist中记录的IP,并将要封的IP写入tempb临时文件
cat whlist|grep -v \# >>tempa
cat whlist|grep -v \# >>tempa
sed 's/^.* //g' tempa|sort|uniq -u|sed'/^$/d' >tempb
#写操作日志
date +%Y%m%d%t%T >>blockip.log
#轮询排重后的可疑IP表开始执行封闭IP操作
while read line
do
/sbin/iptables -I INPUT -s $line -i eth0 -jDROP
done < /root/tempb
#将封闭IP的明细记入日志文件
cat tempa|sort|uniq -u >>blockip.log
#执行封闭操作后给管理员发邮件,要求本机上的sendmail启动
mail -s "Block IP List" admin@testmail.com < tempa
白名单文件列表
# cat whlist
#北京市谷歌(中国)公司
203.208.60.195
203.208.60.163
#sogou蜘蛛
220.181.94.237
#hao260公司
118.192.35.10
#美国华盛顿州雷德蒙德市Microsoft公司
207.46.192.66
#浙江省杭州市阿里巴巴
页:
[1]