A script for downloading and uploading bandwidth limits implemented with Tc+iptables
A script for downloading and uploading bandwidth limits implemented with Tc+iptables
This is for me, New Year for you. Each IP individual limit, good at any time to modify. If you are using dial-up Internet, please add the following content to the/etc/ppp/ip-up.local, otherwise there will be no upload limit when the line is redial, it is not enough to download the BT light limit.
#!/bin/bash
#
#lvhe888 @163.com
#
# define upper and lower bandwidth
# attention is kbit
Download=800kbit
Upload=160kbit
# define intranet IP segment
inet=192.168.0.
# define the restricted IP range
Ips=1
ipe=253
# define this server IP
serverip=254
# define Access Equipment
Idev=eth0
Odev=ppp0
#
#
#
/SBIN/TC Qdisc del Dev $IDEV root handle 10:
/SBIN/TC Qdisc del Dev $ODEV root handle 20:
#
/SBIN/TC qdisc add dev $IDEV root handle 10:cbq bandwidth 100Mbit AVPKT 1000
/SBIN/TC qdisc add dev $ODEV root handle 20:cbq bandwidth 1Mbit AVPKT 1000
#
/SBIN/TC class Add dev $IDEV parent 10:0 classid 10:1 CBQ bandwidth 100Mbit rate 100Mbit Allot 1514 weight 1Mbit Prio 8 ma Xburst AVPKT 1000
/SBIN/TC class Add dev $ODEV parent 20:0 classid 20:1 CBQ bandwidth 1Mbit rate 1Mbit Allot 1514 weight 10Kbit Prio 8 Maxbu RST AVPKT 1000
#
# do not limit intranet downloads from this server.
# Note If there is a proxy on this server, the user can bypass the bandwidth limit by proxy,
# you can cancel the following three sentences to download from this server.
/SBIN/TC class Add dev $IDEV parent 10:1 classid 10:10 CBQ bandwidth 100Mbit rate 95Mbit Allot 1514 weight 20Kbit Prio 5 m Axburst AVPKT 1000 bounded
/SBIN/TC Qdisc Add dev $IDEV parent 10:10 sfq Quantum 1514b perturb 15
/SBIN/TC filter Add dev $IDEV parent 10:0 protocol IP prio u32 match ip src $INET $serverip flowid 10:10
#
#限制下载速度
Counter= $IPS
While [$COUNTER-le $IPE]
Todo
# The following three sentences limit the download bandwidth of each IP
/SBIN/TC class Add dev $IDEV parent 10:1 classid 10:1$counter CBQ bandwidth 100Mbit rate $DOWNLOAD Allot 1514 weight 20Kbi T Prio 5 maxburst avpkt 1000 bounded
/SBIN/TC Qdisc Add dev $IDEV parent 10:1$counter sfq Quantum 1514b perturb 15
/SBIN/TC filter Add dev $IDEV parent 10:0 protocol IP prio u32 match IP dst $INET $counter flowid 10:1$counter
Counter= ' expr $COUNTER + 1 '
Done
#
#限制上传速度
Counter= $IPS
While [$COUNTER-le $IPE]
Todo
# The following three sentences limit the upload bandwidth of each IP
/SBIN/TC class Add dev $ODEV parent 20:1 classid 20:1$counter CBQ bandwidth 1Mbit rate $UPLOAD Allot 1514 weight 4Kbit pri o 5 maxburst avpkt 1000 bounded
/SBIN/TC Qdisc Add dev $ODEV parent 20:1$counter sfq Quantum 1514b perturb 15
/SBIN/TC filter Add dev $ODEV parent 20:0 protocol IP prio handle $COUNTER FW classid 20:1$counter
Counter= ' expr $COUNTER + 1 '
Done
#特殊照顾的IP在以上范围的用户
nip=78
#192.168.0.78 this guy every day BT
Nd=200kbit
Nu=50kbit
/SBIN/TC class Change Dev $IDEV parent 10:1 classid 10:1$nip Bandwidth 100Mbit rate $ND Allot 1514 weight 20Kbit Prio 5 ma Xburst AVPKT 1000 bounded
/SBIN/TC class Change Dev $ODEV parent 20:1 classid 20:1$nip CBQ bandwidth 1Mbit rate $NU Allot 1514 weight 4Kbit Prio 5 m Axburst AVPKT 1000 bounded
#
Nip=1
# 192.168.0.1 increases my own bandwidth
Nd=1500kbit
Nu=500kbit
/SBIN/TC class Change Dev $IDEV parent 10:1 classid 10:1$nip Bandwidth 100Mbit rate $ND Allot 1514 weight 20Kbit Prio 5 ma Xburst AVPKT 1000 bounded
/SBIN/TC class Change Dev $ODEV parent 20:1 classid 20:1$nip CBQ bandwidth 1Mbit rate $NU Allot 1514 weight 4Kbit Prio 5 m Axburst AVPKT 1000 bounded
# ...................
#
#
# Modify the firewall, increase the upload limit
Counter= $IPS
While [$COUNTER-lt $IPE]
Todo
Iptables-t mangle-a prerouting-i $IDEV-S $INET $counter-j MARK--set-mark $COUNTER
Counter= ' expr $COUNTER + 1 '
Done
# this is Nat.
Iptables-t nat-a postrouting-o $EXTIF-S 192.168.0.0/24-j Masquerade
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.