通过企业网站的日志文件分析,对于每日访问量到达一定数量的IP进行查封
脚本通过传参的形式,可以对任何日志进行传递分析
ipt函数进行查封,del函数对昨天查封的IP进行解封
#!/bin/bash #2017-08-12 if [ $# -lt 1 ];then echo "USAGE:$0 ARG" exit 1 fi ipt(){ awk ‘{print $1}‘ $1|sort|uniq -c|sort -rn -k1 > /tmp/tmp.log exec</tmp/tmp.log while read line do ip=`echo $line|awk ‘{print $2}‘` if [ `echo $line|awk ‘{print $1}‘` -gt 10 -a `iptables -L -n|grep "$ip"|wc -l` -lt 1 ];then iptables -I INPUT -s $ip -j DROP echo $ip >>/tmp/ip_$(date +$F).log fi done } del(){ touch /tmp/ip_$(date +$F -d ‘-1day‘).log exec < /tmp/ip_$(date +$F -d ‘-1day‘).log while read line do if [ `iptables -L -n|grep "$line"|wc -l` -ge 1 ];then iptables -D INPUT -s $line -j DROP fi done } main(){ while true do ipt $1 sleep 5 del done } main $*
可以通过加入定时任务方便执行
测试执行的话可以sh <script>.sh XX_2017_xx_xx.log执行,并启动另一个shell
打开watch iptables -L -n进行实时查看,如果超过一定数量的IP进入了防火墙drop列表则表明脚本执行成功。
本文出自 “wxtan” 博客,请务必保留此出处http://wxtan.blog.51cto.com/13124984/1955818
原文:http://wxtan.blog.51cto.com/13124984/1955818