美文网首页Shell
(告诉你什么才是Shell编程二)Shell防范DDos攻击

(告诉你什么才是Shell编程二)Shell防范DDos攻击

作者: 我是李小胖 | 来源:发表于2019-08-08 17:29 被阅读0次
#!/bin/bash
FilePath="access.log"
awk '{print $1}' $FilePath | sort -rn | uniq -c >ip_count.log
cat ip_count.log | while read text  ####读取文件内容,以行为单位
do
echo $text
count=`echo $text | awk '{print $1}' `
ip=`echo $text | awk '{print $2}'`
if [ $count -gt 20 ]
then
if iptables -L | grep $ip  ###判断是否已经在iptables 中
then
echo "ip地址存在iptables中,不添加 "
else
echo "添加ip地址到iptables"
iptables -A INPUT -s $ip -j DROP && echo $ip >>ip_drop.log
/etc/init.d/iptables save &> /dev/null  ###使iptables 生效
/etc/init.d/iptables restart &> /dev/null
fi
else
echo "未到达标准,不添加到iptables"
fi
done

相关文章

网友评论

    本文标题:(告诉你什么才是Shell编程二)Shell防范DDos攻击

    本文链接:https://www.haomeiwen.com/subject/cdqfjctx.html