合格linux运维人员必会的30道shell编程实践题及讲解-06

企业实战题6:请用至少两种方法实现!
写一个脚本解决DOS攻击生产案例
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。

我的脚本=========================

#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
log=/tmp/dos.log
[ -f $log ] || touch $log
function add_iptables(){
while read line
do
    ip=`echo $line|awk '{print $2}'`
    count=`echo $line|awk '{print $1}'`
    if [ $count -gt 100 ] && [ `iptables -L -n|grep "$ip"|wc -l` -lt 1 ];then
        iptables -I INPUT -s $ip -jDROP
        echo "$line isdropped" >> /tmp/droplist.log
    fi
done < $log
}
function main(){
while true 
do
    netstat -an |grep EST|awk -F '[:]+' '{print $6}'|sort|uniq -c > $log
    add_iptables
    sleep 180
done
}
main
原文地址:https://www.cnblogs.com/oliver-blogs/p/7716383.html