shell示例6

  1. 编写脚本/root/bin/checkip.sh,每 5 分钟检查一次,如果发现通过 ssh 登录失败 次数超过 10 次,
    自动将此远程 IP 放入 Tcp Wrapper 的黑名单中予以禁止防问

    !/bin/bash

    while true;do
    mounth=$(date +%b)
    mday=$(date +%d)
    hour=$(date +%H)
    min=$(date +%M)
    IP=awk ' /sshd.*Failed password/ { if("'$mounth'" == $1 && "'$mday'" == $2){ split($3,array,":") # 时间判断5min内出现错误的次数 if("'$hour'" == array[1]){ if("'$min'" < array[2] + 5) allert[$11]++; } else{ if("'$min'"+60-array[2] < 5) allert[$11]++; } } } END{ # 打印结果: 出现大于10次的 IP for(IP in allert){ if(allert[IP]>10) print(IP) } } ' /var/log/secure
    for I in $IP;do
    echo "sshd: ${I}" >> /etc/hosts.deny
    done
    sleep $((5*60))
    done

  2. 配置 magedu 用户的 sudo 权限,允许 magedu 用户拥有 root 权限

    visudo

    magedu ALL=(ALL) ALL

posted @ 2019-12-15 10:55  stars_wisper  阅读(146)  评论(0)    收藏  举报