Akiraka Akiraka
  • 首页
  • Linux
    • ELK
    • PHP
    • Shell
    • Nginx
    • Docker
      • Docker Compose
    • Centos
    • Ubuntu
    • Jenkins
  • Python
  • Mac OS
  • Windows
  • 大数据
    • Hadoop
    • CDH
    • Hive
    • Spark
    • ZooKeeper
  • 微服务
    • Kubernetes
    • Helm
  • Other
  • Quark
  • 首页
  • Linux
    • ELK
    • PHP
    • Shell
    • Nginx
    • Docker
      • Docker Compose
    • Centos
    • Ubuntu
    • Jenkins
  • Python
  • Mac OS
  • Windows
  • 大数据
    • Hadoop
    • CDH
    • Hive
    • Spark
    • ZooKeeper
  • 微服务
    • Kubernetes
    • Helm
  • Other
  • Quark
首页 › Linux › 按时间段拉黑Nginx IP地址

按时间段拉黑Nginx IP地址

Akiraka1年前

功能描述

  • 内网地址白名单
  • 外网地址白名单
  • 设置查找日志时间范围,单位分钟
  • 设置拉黑阈值
  • 不重复添加已经添加过的地址
  • 次日自动释放拉黑名单
  • 判断是否需要执行 nginx -s reload 命令
#!/bin/bash

#   日志存放目录
LOGS=/data/nginx/wwwlogs/
#   Nginx目录
NGINX_BLOCKIP=/data/nginx/vhost/blockip.conf
#   开始查找时间
STERT_TIME=`date -d "-1 minutes" "+%H:%M:00"`
#   结束查找时间
END_TIME=`date "+%H:%M:%S"`
#   设置访问次数,超出设置值拉黑
#   获取系统当前时间
SYS_TIME=`date "+%Y-%m-%d %H:%M:%S"`
#   设置访问次数超过设置次数就拉黑
COUNT="500"
#   设置内网网白名单
FILTER_1="127.0.0.1|localhost|172.16.8.*"
#   设置外网白名单
FILTER_2="47.97.250.22|47.99.91.29|47.111.151.159|116.62.115.60|47.110.254.174|120.26.218.237|120.26.75.197"
#   过滤已经拉过黑的 IP 地址
FILTER_3=`grep -Ev "^$|^[#;]" "${NGINX_BLOCKIP}" | sed "s/;/ /g" | awk '{printf $2 "|"}'`

#判断是否有blockip.conf文件
if [[ ! -f ${NGINX_BLOCKIP} ]];then
  touch ${NGINX_BLOCKIP}
  echo "${NGINX_BLOCKIP} 文件创建成功!"
else
  echo "${NGINX_BLOCKIP} 文件已经存在,无需创建!"
fi

#   批量查询 Nginx 日志
find ${LOGS} -type f -name "*_nginx.log" -exec cat {} \; | 
#   查找指定范围时间日志
awk -F '":"|","|T|\+' '$3 >="'${STERT_TIME}'" && $3 <="'${END_TIME}'"' | 
#   将客户端 IP 地址筛选
awk -F '":"|","' '{print $8}' | 
#   统计 IP 次数排序
sort | uniq -c | sort -rn | 
#   排查白名单中的 IP 地址,FILTER_3 因为拼接问题所以要这样写
grep -Ev "${FILTER_1}|${FILTER_2}|${FILTER_3}null" | 
#   判断是否超出设定值,并输出到nginx配置文件
awk '{if($1>'${COUNT}')print "# '"${SYS_TIME}"' 次数: "$1" \n deny "$2";"}' >> ${NGINX_BLOCKIP}

#   判断 blockip.conf 是否过期,过期自动生成一个新的
if [ $(stat ${NGINX_BLOCKIP} | awk 'NR==6 {print $2}') == $(date +%Y-%m-%d) ];then
    echo $(date "+%Y-%m-%d %H:%M:%S") "不需要删除" >> /root/t.t
else
    echo $(date "+%Y-%m-%d %H:%M:%S") "需要删除" >> /root/t.t
    echo "# ${SYS_TIME}" > ${NGINX_BLOCKIP}
fi

#   判断是否需要加载 Nginx 配置
if [ $( stat ${NGINX_BLOCKIP} | awk 'NR==6 {split($3,arrar,".");print arrar[1]}' ) == $(date +%H:%M:%S) ];then
    echo $(date "+%Y-%m-%d %H:%M:%S") "需要执行 Nginx -s reload 命令" >> /root/t.t
    #   加载Nginx配置
    docker exec -it nginx nginx -s reload
else
    echo $(date "+%Y-%m-%d %H:%M:%S") "无需执行 Nginx -s reload 命令" >> /root/t.t
fi
#linux#shell
0
分享
Akiraka 站长
文章 121评论 10
相关文章
  • Docker 修改默认存储路径
  • Centos/Ubuntu Linux 系统 根目录扩容方法
  • 搭建 Redis 主从搭建配置
  • Ubuntu 与 Centos Vim 配置
  • Docker 常用命令
Akiraka
站长
本人擅长 Ai、Au、Fl、Ae、Pr、Ps 等软件的安装与卸载,精通 CSS、JavaScript、PHP、Python、Shell、Go 等单词的拼写,熟悉 Windows、Linux、Mac、Android、IOS 等系统的开关机!
121文章
10评论
47获赞
想找什么搜一下
文章聚合
密码保护:Kubeadm-etcd 备份
6月前
Linux 下删除乱码文件、目录
1年前
ELK 容器配置
1年前
Kubernetes(k8s)删除节点与添加节点
1年前
Kubernetes(k8s)helm 搭建 prometheus + Grafana 监控
1年前
微信小程序
微信小程序
标签云
brew cdh centos cmd docker docker compose elasticsearch elk filebase filebeat gitlab grafana hadoop helm hive hue jdk jenkins k8s kibana kubernetes liniux linux logstash mac mikrotik mydumper mysql nfs nginx openssl oss php pip pyspark python RabbitMQ redis rocketmq shell Spark ubuntu windows ZooKeeper 容器编排
Copyright © 2019-2021 Akiraka. Designed by nicetheme. 沪18036911号