3 Star 16 Fork 13

aqztcom / cc_iptables

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cc_iptables2.sh 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
ppabc 提交于 2014-10-16 13:38 . update
#!/bin/bash
##http://yzs.me/2050.html
#函数ban_now
ban_now() {
#输出IP的内容
echo $1
#执行iptables对该IP封禁
iptables -I INPUT -s $1 -p all -j DROP
#封禁后执行mail命令,给指定邮箱发一封邮件
echo -e "IP:$1 was banned at $(date).\n\niptables filter tables:\n\n$(iptables -L -n -t filter)" | mail -s "IP:$1 was banned at $(date)" your@email.com
}
#循环的开始
while [ "$loop" = "" ]
do
#清空日志文件
cat>/var/log/nginx/iponly.log<<EOF
EOF
#延迟五秒
ping -c 5 127.0.0.1 >/dev/null 2>&1
#合并,排序IP,输出获取请求数最大的IP及其请求数,请求数与IP之间使用英文逗号隔开,然后赋值给connections
connections=$(cat /var/log/nginx/iponly.log | sort -n | uniq -c | sort -nr | awk '{print $1 "," $2}')
#判断变量connections是否为空
if [ "$connections" != "" ];then
#输出变量connections的内容
echo $connections
#连接数的for循环开始
for ipconntctions in $connections
do
#截取连接数
connectnumber=$(echo $ipconntctions | cut -d "," -f 1)
#判断该IP连接数是否大于200
test $connectnumber -ge 200 && banit=1
#大于200,把IP赋值给变量fuckingip
if [ "$banit" = "1" ];then
fuckingip=$(echo $ipconntctions | cut -d "," -f 2)
ban_now $fuckingip
unset banit
else
#否则,结束for循环
break
fi
done
fi
done
Shell
1
https://gitee.com/null_564_0890/cc_iptables.git
git@gitee.com:null_564_0890/cc_iptables.git
null_564_0890
cc_iptables
cc_iptables
master

搜索帮助