- 编辑脚本,失败三次进入黑名单/etc/hosts.deny(如果没有这文件就创建)
创建黑名单文件
touch /etc/hosts.deny
vim /usr/local/bin/secure_ssh.sh
#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.txt
for i in `cat /usr/local/bin/black.txt`
do
IP=`echo $i |awk -F= '{print $1}'`
NUM=`echo $i|awk -F= '{print $2}'`
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
- 创建纪录失败次数的文件
touch /usr/local/bin/black.txt
- 创建定时任务
crontab -e
*/5 * * * * sh /usr/local/bin/secure_ssh.sh
tips
:wq #保存退出
i #进入插入模式
esc #切换为命令模式