练习1.
在/var/log/secure查找连接失败的主机,并统计次数,累计连接失败超过3次,将此主机加入系统黑名单(/etc/hosts.deny)
#!/bin/bash
cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort |uniq -c |awk '{print $2"="$1}' >/tmp/blacklist
MAXCOUNT="3"
for i in `cat /tmp/blacklist`
IP=`echo $i | awk -F= '{print $1}'`
NUM=`echo &i |awk -F= '{print $2}'`
if [ $NUM -gt $MAXCOUNT ];then
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];then
echo "ssd:$IP" >> /etc/hosts.deny
fi
fi
done