zoukankan      html  css  js  c++  java
  • shell习题第14题:

    【题目要求】

    需求,根据web服务器的访问日志,把一些请求高的ip给拒绝掉,并且每隔半小时把不再发起请求或者请求量很小的ip给解封

    假设:

    1. 一分钟内请求量高于100次的ip视为不正常的请求

    2. 访问日志路径为/data/logs/access_log

    取线上的Nginx日志作为练习

    【核心要点】

    统计ip访问次数,排序

    如何标记每隔半个小时

    iptables计数器是一个重要的判断标准

    函数(封ip,解封ip)

    【脚本】

    block_ip()
    {
        t1=`date -d "-1 min" +%Y:%H:%M`
        log=/data/logs/access_log
    
        egrep "$t1:[0-9]+" $log > /tmp/tmp_last_min.log
        awk '{print $1}' /tmp/tmp_last_min.log | sort -n | uniq -c | sort -n | awk '$1>100 {pr
    int $2}' > /tmp/bad_ip.list
        n=`wc -l /tmp/bad_ip.list | awk '{print $1}'`
        if [ $n -ne 0 ]; then
            for ip in `cat /tmp/bad_ip.list`
            do
                iptables -I INPUT -s $ip -j REJECT
            done
        fi
    }
    
    unblock_ip()
    {
        iptables -nvL INPUT | sed '1d' | awk '$1<5 {print $8}' > /tmp/good_ip.list
        n=`wc -l /tmp/good_ip.list | awk '{print $1}'`
        if [ $n -ne 0 ];then
            for ip in `cat /tmp/good_ip.list`
            do
                iptables -D INPUT -s $ip -j REJECT
            done
    
        fi
        iptables -Z
    }
    
    t=`date +%M`
    if [ $t == "00" ] || [ $t == "30" ];then
        unblock_ip
        block_ip
    else
        block_ip
    fi
  • 相关阅读:
    ds
    使用js做的贪吃蛇游戏的知识总结
    使用js实现的关联表单
    使用jquery实现的计算器功能
    vue学习(2)关于过渡状态
    vue学习(1)生命周期
    vue学习(0)写在前面的一句话
    bootstrap(7)关于进度条,列表,面板
    bootstrap(6)分页,翻页,徽章效果
    异常捕获
  • 原文地址:https://www.cnblogs.com/dingzp/p/10990751.html
Copyright © 2011-2022 走看看