#!/bin/bash
##http://yzs.me/2050.html
#ban_now
ban_now() {
#IP
echo $1
#ִiptablesԸIP
iptables -I INPUT -s $1 -p all -j DROP
#ִmailָ䷢һʼ
echo -e "IP:$1 was banned at $(date).\n\niptables filter tables:\n\n$(iptables -L -n -t filter)" | mail -s "IP:$1 was banned at $(date)" your@email.com
}
#ѭĿʼ
while [ "$loop" = "" ]
do
#־ļ
cat>/var/log/nginx/iponly.log<<EOF
EOF
#ӳ
ping -c 5 127.0.0.1 >/dev/null 2>&1
#ϲIPȡIPIP֮ʹӢĶŸȻֵconnections
connections=$(cat /var/log/nginx/iponly.log | sort -n | uniq -c | sort -nr | awk '{print $1 "," $2}')
#жϱconnectionsǷΪ
if [ "$connections" != "" ];then
#connections
  echo $connections
#forѭʼ
  for ipconntctions in $connections
    do
#ȡ
      connectnumber=$(echo $ipconntctions | cut -d "," -f 1)
#жϸIPǷ200
      test $connectnumber -ge 200 && banit=1
#200IPֵfuckingip
      if [ "$banit" = "1" ];then
          fuckingip=$(echo $ipconntctions | cut -d "," -f 2)
          ban_now $fuckingip
          unset banit
      else
#򣬽forѭ
          break
      fi
     done
fi
done