#! /bin/bash
filepath='/var/log/'
filename='secure'
cat ${filepath}${filename} | awk '/Failed/{if($(NF-3)!="session:"){print $(NF-3)}}' | sort | uniq -c | awk '{print $2"="$1;}' > ${filepath}tmp.${filename}
for i in `cat ${filepath}tmp.${filename}`
do
IP=`echo $i |awk -F= '{print $1}'`
NUM=`echo $i|awk -F= '{print $2}'`
if [ $NUM -gt 30 ];then
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];then
echo "sshd:$IP:deny" >> /etc/hosts.deny
fi
fi
done
思路:
centos7禁止指定IP访问,只需要将IP添加到/etc/hosts.deny即可。格式:sshd:$IP:deny
vim /etc/hosts.deny
sshd:192.168.0.1:deny
同理,添加允许的IP:
vim /etc/hosts.allow
sshd:192.168.0.1:allow