Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wenjun1055/8131112 to your computer and use it in GitHub Desktop.

Select an option

Save wenjun1055/8131112 to your computer and use it in GitHub Desktop.

Revisions

  1. @spidersea spidersea renamed this gist Dec 24, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @spidersea spidersea created this gist Dec 24, 2013.
    34 changes: 34 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/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