转载:https://blog.csdn.net/xiezuoyong/article/details/77322528
Shell编程面试题6_监控IP连接数,超过100封掉该IP
老男孩出的Shell编程企业面试题6:
写一个脚本解决DOS攻击生产案例 提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。
我的Shell脚本如下:
#!/bin/bash
#environment variable
source /etc/profile
iplist=`netstat -ntu | awk '{print $5}'| cut -d':' -f1| sort |uniq -c | sed 'N;$d;P;D' | awk '{if($1>100)print $2}'`
for ip in $iplist
do
iptables -I INPUT -s $ip -j DROP
echo "$ip is drop!"
done
执行crontab -e,设置每3分钟执行一次:
crontab -e
*/3 * * * * /bin/bash /test/shellstudy/netstatip.sh