#!/bin/bash #======================================================================================== ## ## FILE: t-shaping ## ## DESCRIPTION: Throttles bandwidth for the specified interface/IP. Designed to be ## placed in /etc/init.d and made executable. ## ## AUTHOR: Scott Sullivan (ssullivan), ssullivan@liquidweb.com ## COMPANY: Liquid Web, Inc. ##======================================================================================= tc_cmd=/sbin/tc interface=eth0 download=200mbit upload=200mbit ip=10.30.104.4 U32="$tc_cmd filter add dev $interface protocol ip parent 1:0 prio 1 u32" #=== FUNCTION ================================================================ ## NAME: exists ## DESCRIPTION: Ensures passed command is in the path. ## PARAMETERS: none ## RETURNS: none ##=============================================================================== function exists { if command -v $1 &>/dev/null then return 1 else echo "$1 was not found, please ensure $1 is installed and in the PATH." exit 255 fi } #=== FUNCTION ================================================================ ## NAME: status ## DESCRIPTION: Show traffic shaping status, directly from tc. ## PARAMETERS: none ## RETURNS: none ##=============================================================================== status() { $tc_cmd -s qdisc ls dev $interface } #=== FUNCTION ================================================================ ## NAME: restart ## DESCRIPTION: Restart traffic shaping. ## PARAMETERS: none ## RETURNS: none ##=============================================================================== restart() { stop sleep 2 start } #=== FUNCTION ================================================================ ## NAME: stop ## DESCRIPTION: Stop traffic shaping. ## PARAMETERS: none ## RETURNS: none ##=============================================================================== stop() { $tc_cmd qdisc del dev $interface root } #=== FUNCTION ================================================================ ## NAME: start ## DESCRIPTION: Start traffic shaping. ## PARAMETERS: none ## RETURNS: none ##=============================================================================== start() { $tc_cmd qdisc add dev $interface root handle 1: htb default 30 $tc_cmd class add dev $interface parent 1: classid 1:1 htb rate $download $tc_cmd class add dev $interface parent 1: classid 1:2 htb rate $upload $U32 match ip dst $ip/32 flowid 1:1 $U32 match ip src $ip/32 flowid 1:2 } exists tc case "$1" in start) echo -n "Starting traffic shaping: " start echo -e '\E[0;32m'"OK" ;; stop) echo -n "Stopping traffic shaping: " stop echo -e '\E[0;32m'"OK" ;; restart) echo -n "Restarting traffic shaping: " restart echo -e '\E[0;32m'"OK" ;; status) echo "Traffic shaping status for $interface:" status echo "" ;; *) pwd=$(pwd) echo "Usage: $0 {start|stop|restart|status}" ;; esac