一、系统:Centos6.3 64位
二、方法:读取/var/log/secure,查找关键字 Failed,例如(注:文中的IP地址特意做了删减):
Sep 17 09:08:09 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2
Sep 17 09:08:20 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2
Sep 17 09:10:02 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2
Sep 17 09:10:14 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2
从这些行中提取IP地址,如果次数达到10次(脚本中判断次数字符长度是否大于1)则将该IP写到 /etc/hosts.deny中。
三、步骤:
1、先把始终允许的IP填入 /etc/hosts.allow ,这很重要!比如:
sshd:19.16.18.1:allow
sshd:19.16.18.2:allow
2、脚本 /usr/local/bin/secure_ssh.sh
#! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}&#