服务器防止并发连接脚本(基于iptables)

发布时间:2020-07-07 20:52:51 作者:xiangys0134
来源:网络 阅读:335

需求:服务器http并发连接数多的就用iptables拒绝掉

思路:

1、首先通过netstat统计当前http连接数(大于3个连接就将连接数统计值和连接IP重定向到/root/black.txt)

2、将白名单IP写入到/root/white.txt(防止之后iptables将一些正常请求的IP拒绝掉)

3、运行命令awk '{print $2}' /root/black.txt 获取打印连接数过高的IP地址

4、定义一个变量dropip,其类型为数组

5、循环数组里面的值( ${dropip[@]}表示数组下标的所有值 )

6、action "拒绝IP${var}" /bin/true 这个被我注释掉了,主要是用来调试的时候用了下



[root@linux-node8 test]# cat iptables.sh 

#!/bin/bash

#

. /etc/init.d/functions

httpcc=`netstat -aon|grep "172.2.0.68:80" |grep  "ffff" |awk '{print $5}' |cut -d":" -f 4 |sort |uniq -c |awk '$1 >3 {printf $1 "\t" $2 "\n"}' >/root/black.txt`

sleep 6

dropip=(`awk '{print $2}' /root/black.txt`)


for var in ${dropip[@]} 

  do

grep "$var" /root/white.txt &>/dev/null

[  $? -ne 0 ] && {

iptables -I INPUT -p tcp -s $var -j DROP 

echo "iptables -I INPUT -p tcp -s $var -j DROP" >> /root/deny_ip.log

#action "拒绝IP${var}" /bin/true

sleep 3

}

  done


推荐阅读:
  1. Centos6配置PPTP连接VPS服务器
  2. shell 脚本应用《三》监控当某个IP并发连接数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

脚本 并发 连接数

上一篇:Linux文本处理工具和正则表达式

下一篇:ubuntu下使用docker安装grafana

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》