Last active
January 1, 2016 07:18
-
-
Save spidersea/8110326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
iptables_ip(){ | |
FILE=$1 | |
egrep -iv "Baiduspider|bingbot|360Spider|JianKongBao|Ezooms|msnbot|Safari|AhrefsBot|Firefox|TurnitinBot|bot|AppleWebKit" $FILE|awk '{print $1}' |sort -n|uniq -c|sort -nr|awk '{if ($1>50) print $1" "$2}' >.tmp_ip | |
if [ -s .tmp_ip ] | |
then | |
while read line | |
do | |
NUMBER=`echo $line|awk '{print $1}'` | |
IP=`echo $line|awk '{print $2}'` | |
NETMAST=`echo $IP|awk -F"." '{print $1"."$2"."$3}'` | |
if grep -q $NETMAST allow_iplist | |
then | |
continue | |
else | |
if ! grep -q $NETMAST deny_iplist | |
then | |
echo $IP >>deny_iplist | |
/sbin/iptables -A INPUT -s ${NETMAST}.0/24 -j DROP | |
NOW_TIME=`date +"%Y%m%d %H:%M:%S"` | |
cat .tmp_ip|mail -s "$NOW_TIME iptables stops IP list" [email protected] | |
fi | |
fi | |
done <.tmp_ip | |
fi | |
exit 0 | |
} | |
iptables_ip /home/wwwroot/xxxx/log/access.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment