10 * * * * /root/sshLoginRestrictions.sh > /dev/null 2>&1 &
sshLoginRestrictions.sh
#! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/black.list for i in `cat /tmp/black.list` do ip=`echo $i |awk -F= '{print $1}'` num=`echo $i|awk -F= '{print $2}'` if [ $num -gt 20 ]; then grep $ip /etc/hosts.deny > /dev/null if [ $? -gt 0 ];then echo "sshd:$ip:deny" >> /etc/hosts.deny fi fi done
解除IP限制:将限制IP加到白名单/etc/hosts.allow
sshd:黑名单IP:allow