Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wenjun1055/8131112 to your computer and use it in GitHub Desktop.
Save wenjun1055/8131112 to your computer and use it in GitHub Desktop.
#!/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