SSH登录失败次数超过10次,自动加黑ip

1.新建文件

vim /root/bin/checkip.sh

 

2.输入代码

#!/bin/sh
#登录失败次数大于10的ip
IP=$(awk '/Failed/{print $(NF-3)}' /var/log/secure | sort |uniq -c |awk '{if($1>10) print $2}')
hostdeny=/etc/hosts.deny
for i in $IP
do
#如果ip不存在,则写入deny文件
if [ ! $(grep $i $hostdeny) ];
then
echo "sshd:$i" >> $hostdeny
fi
done

 

3.赋予权限

chmod 777 /root/bin/checkip.sh

 

4.编辑计划任务

crontab -e

 

5.添加计划

*/5 * * * * /root/bin/checkip.sh &>/dev/null

 

6.退出后,重启计划任务

systemctl reload crond
systemctl restart crond

 

posted @ 2022-10-24 11:14  null_v  阅读(296)  评论(0)    收藏  举报