#!/bin/bash
DATE=$(date +%d/%b/%Y:%H:%M)
LOG_FILE=/usr/local/nginx/logs/access.log #通过访问日志提取IP地址
ABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}') #对于当前访问时间超过10此此标记为非法IP
for IP in $ABNORMAL_IP; do
if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then
iptables -I INPUT -s $IP -j DROP #对非法IP的访问流量进行丢弃
echo "$(date +'%F_%T') $IP" >> /tmp/drop_ip.log
fi
done
标签:iptables,Shell,LOG,IP,FILE,防范,DATE,Dos
From: https://www.cnblogs.com/tjane/p/16901288.html