您好,登錄后才能下訂單哦!
需求:服務(wù)器http并發(fā)連接數(shù)多的就用iptables拒絕掉
思路:
1、首先通過netstat統(tǒng)計當(dāng)前http連接數(shù)(大于3個連接就將連接數(shù)統(tǒng)計值和連接IP重定向到/root/black.txt)
2、將白名單IP寫入到/root/white.txt(防止之后iptables將一些正常請求的IP拒絕掉)
3、運(yùn)行命令awk '{print $2}' /root/black.txt 獲取打印連接數(shù)過高的IP地址
4、定義一個變量dropip,其類型為數(shù)組
5、循環(huán)數(shù)組里面的值( ${dropip[@]}表示數(shù)組下標(biāo)的所有值 )
6、action "拒絕IP${var}" /bin/true 這個被我注釋掉了,主要是用來調(diào)試的時候用了下
[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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。