Recently, I started toying with the concept of dynamically changing the bandwidth instead of using a fixed value. The idea was, for those users who don't consume too much bandwidth within a specified time period, they will be rewarded with a higher bandwidth allocation as compared to those who constantly consume bandwidth within the same time period.
For example, if a user casually spends more time reading website content, less bandwidth is used. As a reward, more bandwidth is allocated for that user so the next time (s)he browses for content, it's a much faster experience. This is in contrast to bandwidth hoggers who constantly download/upload content (e.g. watching video-streaming sites or multi-megabyte file transfers). To lessen the lag other non-hogging users might potentially experience, bandwidth hoggers get allocated lesser bandwidth as penalty.
I wrote the following script that implements this concept. It constantly monitors the bandwidth and changes the allocation depending on the amount of activity during a time period. It's flexible enough to allow changes to settings on-the-fly. The relevant settings are stored in a separate configuration file. You can edit the values to your liking. Before you run the script, make sure the location of the configuration file is referenced correctly in the script by editing the following line:
- Code: Select all
CONFIG_FILE="/path/to/dynamic-shaper.settings"
Before you can use this script, you'll need to install wondershaper:
- Code: Select all
sudo apt-get install wondershaper
To see it in action, open a terminal session where you saved the files:
- Code: Select all
chmod +x dynamic-shaper.sh
sudo ./dynamic-shaper.sh
It will run in an infinite loop. Start your browser and connect to some video-streaming site. Depending on your settings, the bandwidth allocation will start to go down and will remain in the lowest setting until the streaming stops. The bandwidth will gradually increase as long as the average rate is within the specified threshold. To stop the script, hit Ctrl+C in the terminal window where the script is running, and type:
- Code: Select all
sudo wondershaper clear <interface-name-here>
to disable bandwidth shaping.
To run the script on startup, save/move the files to the /root folder, and edit root's crontab settings:
- Code: Select all
sudo crontab -u root -e
- Code: Select all
# m h dom mon dow command
@reboot /root/dynamic-shaper.sh > /dev/null 2>&1
Save the cron table and reboot the computer.
I hope this is useful or helpful to someone who's in a similar boat.
Thanks to Vincent Vermeulen for the heads-up about the nstat command.
Script and sample configuration file on the next post.

