tcp wrapper实现,其配置文件是 /etc/hosts.{deny|allow}
sshd:100.1.1.1:deny
#服务名:IP:deny, :deny 可以省略,
#IP可以匹配通配符,如 8.8.8.*, 也可以使用掩码或prefix,如 8.8.8.0/24
#当 hosts.deny 和 hosts.allow 两个文件中都有某个IP时,优先allow
#在Centos6或更高版本中,保存 hosts.{deny|allow} 文件后立即生效
脚本:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cat /var/log/secure | awk '/Failed/{print (NF-3)}' | sort | uniq -c | awk '{print2"="1;}'>/root/sshblack.txt
LIMIT=20
for i in `cat /root/sshblack.txt`
do
IP="`echoi | awk -F = '{print 1}'`"
TIMES="`echoi | awk -F = '{print 2}'`"
if [TIMES -ge LIMIT ]; then
grepIP /etc/hosts.deny > /dev/null
if [ ? != 0 ]; then
echo "sshd:IP:deny" >> /etc/hosts.deny
fi
fi
done
#如果添加到crontab, 要了解是否执行成功?那么可以添加一行日志
if [ ? -eq 0 ]; then
echo "`date +"%b %d %T"`HOSTNAME USER ssh_cron.sh finished." >> /var/log/messages
else
echo "`date +"%b %d %T"`HOSTNAME $USER ssh_cron.sh failed." >> /var/log/messages
增加crontab:
00 2 * * * sh /root/ssh_cron.sh > /dev/null 2>&1