1. 程式人生 > >過濾nginx日誌並添加防火墻拒絕訪問 centos7

過濾nginx日誌並添加防火墻拒絕訪問 centos7

gin address top 結束 ESS all 文件 print ddr

#/bin/bash
#日誌文件
logfile=‘/var/log/nginx/access.log‘

hours=1

#開始時間
start_time=date -d "-$hours hour" +"%H:%M:%S"
#echo $start_time

#結束時間
stop_time=date +"%H:%M:%S"

#echo $stop_time
#過濾出單位之間內的日誌並統計最高ip數
array=($(tac $logfile | awk -v st="$start_time" -v et="$stop_time" ‘{t=substr($4,RSTART+14,21);if(t>=st && t<=et) {print $0}}‘ \

| awk ‘{print $1}‘ | sort | uniq -c | sort -nr |awk ‘{if($1 > 5000){print $0}}‘|awk ‘{print $2}‘))

wait

for each in ${array[@]}
do

echo $each

firewall-cmd --permanent --add-rich-rule="rule family=‘ipv4‘ source address=‘$each‘ reject"

done

firewall-cmd --reload

過濾nginx日誌並添加防火墻拒絕訪問 centos7