禁止ssh暴力破解

linux服务器一旦有公开IP,查一查日志,就有大量的ssh登陆尝试。

癞蛤蟆趴脚面,不咬人,膈应人。

禁止ssh暴力破解脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
#! /bin/bash
cat /var/log/auth.log|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/block.txt
for i in `cat /tmp/block.txt`
do
IP=`echo $i |awk -F= '{print $1}'`
NUM=`echo $i|awk -F= '{print $2}'`
if [ ${NUM} -gt 1 ]; then #登录失败大于1次,即加入黑名单
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];then
echo "sshd:$IP:deny" >> /etc/hosts.deny
fi
fi
done