脚本禁止登录尝试

2022/09/22 posted in  网络相关

需求

有ip不停的尝试登录,可以通过防火墙进行屏蔽

脚本

cat /var/log/secure |grep failure|awk '{for (i=1;i<=NF;i++){if ($i ~/rhost/) {print $i}}}'| sort | uniq|awk -F"=" '{print $2}' >> black.list

cat black.list|sort|uniq > black.list.tmp
rm -rf black.list
mv black.list.tmp black.list

firewall-cmd --list-rich-rules > blackiprule.list
all=`cat blackiprule.list`

for ip in `cat black.list|sort|uniq`
do
if [[ $all =~ $ip ]]
then
:
else
	echo "不包含"
	firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='$ip' reject"
fi
done

cat blackiprule.list|wc -l
firewall-cmd --reload

定期执行脚本即可