需求:请根据web日志或者或者网络连接数,监控当某个ip并发连接数或者短时内pv达到100,即调用防火墙命令封掉对应的ip。
防火墙命令为:iptables-i input -s ip地址 -j drop。
脚本实现
#!/bin/bash
info_file=/tmp/ddos_check.log
#从连接数获取
#netstat -lant|awk -f "[ :]+" '/180:80/{clsn[$6]++}end{for(pol in clsn)print pol,clsn[pol]}' >$info_file
# 从日志获取
awk '{hotel[$1]++}end{for(pol in hotel)print pol,hotel[pol]}' access.log | sort -nk2 -r >$info_file
while read line
do
ip_add=`echo $line |awk '{print $1}'`
access=`echo $line |awk '{print $2}'`
if [ $access -ge 10000 ]; then
iptables -i input -s $ip_add -j drop
elif [ $access -le 5000 ]; then
echo "小于5000"
else
echo "大于5000小于10000"
fi
done <$info_file
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论