#!/bin/bash
local_ip="192.168.0.4" #定义本地IP不会被拒绝
tmp_log=`mktemp`
cat /var/log/secure |grep "Failed password for root from" |awk ‘{print $11}‘ |uniq -c > $tmp_log
cat $tmp_log |while read line
do
attack_num=`echo $line |awk ‘{print $1}‘`
attact_ip=`echo $line |awk ‘{print $2}‘`
if [ "$attack_num" -gt "10" ] && [ "$local_ip" != "$attack_ip" ]
then
echo sshd:"$attact_ip":deny >> /etc/hosts.deny
fi
done
sort -u /etc/hosts.deny > $tmp_log #过滤相同IP
cp -f $tmp_log /etc/hosts.deny
rm -f $tmp_log
每5分钟检测一次,在5分钟内尝试密码登录错误超过10次的拒绝这个IP
crontab -e
*/5 * * * * /sh/sshtool.sh
查看被拒绝的IP
cat /etc/hosts.deny
时间: 2024-12-23 14:59:06