防火墙脚本, 通过access.log 封锁IP

  1. #!/bin/bash  

  2. ACCCESS_LOG=/tmp/access.log  

  3. TIMEPOINT='24/May/2012'

  4. BLACKLIST=/var/tmp/black  

  5. WHITELIST=/var/tmp/white  

  6. if [ ! -f ${BLACKLIST} ]; then  

  7.    touch ${BLACKLIST}  

  8. fi  

  9. if [ ! -f ${WHITELIST} ]; then  

  10.    touch ${WHITELIST}  

  11. fi  

  12. for deny in $(grep ${TIMEPOINT} ${ACCCESS_LOG} | awk '{print $1}' | awk -F'.''{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -r -n | head -n 30| awk '{print $2}')  

  13. do

  14. if [ $(grep -c $deny ${WHITELIST}) -ne 0 ]; then  

  15. echo'Allow IP:'$deny

  16.    iptables -D INPUT -p tcp --dport 443 -s $deny -j DROP  

  17.    iptables -D INPUT -p tcp --dport 80 -s $deny -j DROP  

  18. continue

  19.    fi  

  20. if [ $(grep -c $deny ${BLACKLIST}) -eq 0 ] ; then  

  21. echo'Deny IP:'$deny

  22. echo$deny >> ${BLACKLIST}  

  23.        iptables -I INPUT -p tcp --dport 443 -s $deny -j DROP  

  24.        iptables -I INPUT -p tcp --dport 80 -s $deny -j DROP  

  25.    fi  

  26. done  


你可能感兴趣的:(封IP,防火墙脚本)