#! /bin/bash #Denyhosts SHELL SCRIPT #2022-9-27 #可以添加定时任务 crontab -l crontab -e */1 * * * * sh /home/shell-code/secure_check.sh #如果担心自己密码输入错误次数多,就把自己的 ip 加入的 hosts.allow里面 cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /home/shell-code/black.txt for i in `cat /home/shell-code/black.txt` do IP=`echo $i |awk -F= '{print $1}'` NUM=`echo $i|awk -F= '{print $2}'` #根据 /var/log/secure 文件,捞取密码失败五次以上IP信息 ,然后写入到/etc/hosts.deny,禁止相关ip ssh访问服务器 if [ $NUM -gt 5 ];then grep $IP /etc/hosts.deny > /dev/null if [ $? -gt 0 ];then echo "sshd:$IP:deny" >> /etc/hosts.deny fi fi done