-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandwidth-limiter-linux
76 lines (63 loc) · 3.5 KB
/
bandwidth-limiter-linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# Linux bash script to limit network interface bandwidth using wondershaper utility (must be installed)
# There is one base limit, and possible also 2 different limits in 2 different timeframes.
# Launch periodicaly (10 minutes?) using cron (crontab of the root user!) to maintain defined limits and/or run manually to set/temporarily override limit
# sample su "crontab -e" line: */10 * * * * /bin/bash /home/userhere/scripts/bandwidth-limiter-linux 2>&1 /dev/null
# Edit following values to define two timeframes and its bandwidth limits, it will be maintained when ran via cron.
# To override these limits, run this script with parameter:
# scriptname bwlup (to increase bandwdith) OR bwldown to decrease. Such override should last maximum 24 hours.
# time frame at which to remove limits, example night to morning: 23:00-7:00:
t1start="230000"
t1end="70000"
# time frame at which to apply most strict limits (in bytes - 35000=3.5MB), example evening: 18:45-21:30:
t2start="184500"
t2end="213000"
strictlimit=35000
# outside of timeframes will be applied limit (in bytes):
baselimit=45000
# custom status file
overridefile=/tmp/bandwidth-limiter-linux-override
sudo touch $overridefile && sudo chown "$(whoami)":"$(whoami)" "$overridefile" 2>/dev/null
override="$(cat "$overridefile")"
currentTime=$(date +"%H%M%S" | bc)
nic=$(ip -o -4 route show to default|head -n1|awk '{print $5}')
#nic=wg0
# (netinterface you can set manually like =eth0 . Above command should extract main interface automatically)
###### Adjust BW limit based on parameter provided to this bash script: ########
# set bandwidth limits in bytes (10000 = ~1 MB/s):
# -d is download limit
# -u is upload limit
if [[ "$1" != "" ]];then
if [[ "$1" == "bwl" ]];then
sudo wondershaper -c -a "$nic";echo bwl>"$overridefile"
sudo wondershaper -a "$nic" -d 15000 -u 55000
#sudo wondershaper -a wlp2s0 -d 2000 -u 2000
elif [[ "$1" == "bwloff" ]];then
sudo wondershaper -c -a "$nic";echo bwloff>"$overridefile"
elif [[ "$1" == "bwlup" ]];then
sudo wondershaper -c -a "$nic";echo bwlup>"$overridefile"
sudo wondershaper -a "$nic" -d 25000 -u 58000
elif [[ "$1" == "bwldown" ]];then
sudo wondershaper -c -a "$nic";echo bwldown>"$overridefile"
sudo wondershaper -a "$nic" -d 10000 -u 30000
fi
else
echo -e "Script requires parameter (./thisscriptname parameter) to change bandwidth limit. Parameters:\nbwl (limit bandwidth) or \nbwloff (to remove limits) or\nbwlup (to increase bandwidth a bit) or\nbwldown (to decrease bandwidth limit)"
fi
######### Adjust BW limit in above defined timeframes: #########
if [[ $currentTime -le $t1start && $currentTime -ge $t1end ]]; # Out of timeframe 1 (night)
then
if [[ "$(wondershaper -a "$nic" -s|grep -c Mbit)" == "0" ]];then # if unlimited
sudo -u "$(whoami)" wondershaper -a "$nic" -d "$baselimit" -u "$baselimit" # add base limit
fi
if [[ $currentTime -ge $t2start && $currentTime -le $t2end ]] && [[ "$override" != "bwlup" ]]; # Within timeframe 2 & user not set override (strict limit)
then
sudo -u "$(whoami)" wondershaper -a "$nic" -d "$baselimit" -u "$baselimit" -c # remove base limit
sudo -u "$(whoami)" wondershaper -a "$nic" -d "$strictlimit" -u "$strictlimit" # add strict limit
fi
else # Within timeframe 1 (no limit)
cat /dev/null > "$overridefile" # make sure that custom/override limit is reset in this daily period so it not spans multiple days
if [[ "$(wondershaper -a "$nic" -s|grep -c Mbit)" != "0" ]];then # wondershaper limit active
sudo -u "$(whoami)" wondershaper -a "$nic" -d "$baselimit" -u "$baselimit" -c # remove limit
fi
fi