shell脚本自动过滤尝试多次连接ip并添加到系统黑名单

#!/bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c | awk '{{ print $2 "=" $1 }}' > /opt/black_list.txt

for  blakc in `cat /opt/black_list.txt`
do
    IP = `echo $black | awk -F= '{ print $1}'`
    SEC = `echo $black | awk -F= '{ print $2}'`
    if [[ $SEC -get 10 ]] ;then
       grep $IP /etc/hosts.deny >/dev/null
        if [[ $? -gt 0 ]];then
          echo 'sshd:$IP:deny' >> /etc/hosts.deny
        fi
     fi   
done

解释:

  第一行过滤secure 日志文件第一个awk 以空格分隔是过滤出以failed 这一行中倒数四列,“”sort“” 排序 "uniq -c"  在每一列的旁边显示次数 第二个awk 调换查询出来的ip 和 次数调换位置并且以等号相连接

       第二行 使用for 循环遍历文件

  第四,五行 过滤出ip和次数

      第一个if  如果次数大于10次先查看 ip 是否已经存在 第一二个if 如果上次结果等于0 则写入hosts.deny 文件

posted @ 2020-11-26 11:53  请别对我太好  阅读(336)  评论(0编辑  收藏  举报