首页 > 其他 > 详细

过滤nginx日志并添加防火墙拒绝访问 centos7

时间:2018-10-08 17:15:07      阅读:269      评论:0      收藏:0      [点我收藏+]
#/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

原文:http://blog.51cto.com/1054054/2295942

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!