参考文章地址:https://yq.aliyun.com/ziliao/48446
1、收集 /var/log/secure 里面的信息,若是某个IP 链接次数超过一定次数 ,则把此ip记录到/etc/hosts.deny里面。
通过crontab来执行,每天的1点1分执行一次。
59 23 * * * /usr/bin/sh /root/bin/Denyhosts.sh
脚本内容如下:
#!/bin/bash #Denyhosts SHELL SCRIPT cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"=" $1;}' >/root/bin/Denyhosts.txt DEFINE="10" for i in `cat /root/bin/Denyhosts.txt` do IP=`echo $i|awk -F= '{print $1}'` NUM=`echo $i|awk -F= '{print $2}'` if [ $NUM -gt $DEFINE ] then grep $IP /etc/hosts.deny >/dev/null if [ $? -gt 0 ]; then echo "sshd:$IP" >> /etc/hosts.deny fi fi done