I'm looking for a way to create a daily internet limit for my Ubuntu Server. All outbound traffic leaves Eth2, a usb dongle, this is prepaid and the kids eat it quickly. I need to divide my prepaid amount usually 12Gb into daily allotments and stop traffic for the day after this amount is reached. Maybe with a web page thrown in saying daily limit exceeded.
Preferably something from the CLI. It's a headless beast with only SSH access.
VNSTAT seems to do what I need, I just don't have the scripting skills to have it drive an ifdown command.
Thanks.
My suggestion is the following script that will get the data of incoming and outgoing traffic from
ifconfig interface-name
and will compare the sum with a predefined limit value. This action will be repeated every 5 seconds (for example).When the amount of the traffic (income+outcome) becomes equal or greater than the limit, the script will disable the target interface and exit. The maximum discrepancy between the actual value at which the interface will be disabled and the limit value will be equal to
5s
xMaxSpeed
.The script can be executed by Cron job. So you will be able to set different job for each day of the week, etc. Additionally when the limit is reached you can run the script manually with an additional amount of traffic.
The script name should be
traffic-watch
, otherwise you should change its 5th line. My suggestion is to place it in/usr/local/bin
, thus it will be available as shell command. Don't forget to make it executable:chmod +x /usr/local/bin/traffic-watch
.The script should be executed as root (
sudo
). It creates a log file:/tmp/traffic-watch-interface-name.log
, where you can check the last action. The script has two input variables:$1
=$LIMIT
- the value of the traffic limit in MB - the default value is400
.$2
=$IFACE
- the name of the target network interface - the default value iseth0
.If you want to override these values during the execution of the script, use these formats:
Use 'traffic-watch' with 'crontab'. If you want to run the script every morning at
6:30
, open root's Crontab (sudo crontab -e
) and add this line:Use 'traffic-watch' manually. To run the script as root and push it into the background we shall use
sudo -b
:The content of the script 'traffic-watch' is:
Notes:
Disable the script when you update and upgrade the system! The lack of internet could be cause of broken packages.
It is a good idea to attempt to kill the previous instance of the script (just in case its limit is not reached) before run a new:
Probably
2>/dev/null
is not obligatory, because, I think all, errors are redirected to/dev/null
by the script itself.To check the remaining traffic remotely you can use this command:
Thanks to @Dessert for this idea! (Replace
eth0
with the actual interface in use.)To get back your network interface UP: First ise
ifconfig -a
to find its name. Thensudo ifconfig INTERFACE up
.This script could be recreated to work with
iptables
instead ofifconfig - up/down
. This will be a powerful solution.The script is available as GitHub repository at: https://github.com/pa4080/traffic-watch
Another script, based on the current, that only will get the traffic for a period of time is provided here: How to get the current network traffic via the commandline in a simple format.
References:
ifconfig