#!/bin/bash
nginxAccessLogFile=`cat /etc/nginx/nginx.conf | grep access_log | grep -v '#' | awk '{print $2}'`
if [[ "$nginxAccessLogFile" = '' ]];then
echo 'nginx access.log not found'
exit
fi
date=$(date +%d/%b/%Y:%H:%M)
limit=100
num=5000
ips=$(tail -$num $nginxAccessLogFile | grep $date | awk -v li="$limit" '{a[$1]++}END{for(i in a)if(a[i]>li)print i}')
for ip in $ips;do
if [[ $(iptables -vnL | grep $ip | wc -l) -eq 0 ]];then
iptables -I INPUT -s $ip -j DROP
echo "$(date +'%F_%T') $ip" >> ./drop_ip.log
fi
done