Bash Script - Traffic Shaping
- by Craig-Aaron
hey all, I was wondering if you could have a look at my script and help me add a few things to it,
How do I get it to find how many active ethernet ports I have? and how do I filter more than 1 ethernet port
How I get this to do a range of IP address?
Once I have a few ethenet ports I need to add traffic control to each one
#!/bin/bash
# Name of the traffic control command.
TC=/sbin/tc
# The network interface we're planning on limiting bandwidth.
IF=eth0 # Network card interface
# Download limit (in mega bits)
DNLD=10mbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=1mbit # UPLOAD Limit
# IP address range of the machine we are controlling
IP=192.168.0.1 # Host IP
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
start() {
# Hierarchical Token Bucket (HTB) to shape bandwidth
$TC qdisc add dev $IF root handle 1: htb default 30 #Creates the root schedlar
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD #Creates a child schedlar to shape download
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD #Creates a child schedlar to shape upload
$U32 match ip dst $IP/24 flowid 1:1 #Filter to match the interface, limit download speed
$U32 match ip src $IP/24 flowid 1:2 #Filter to match the interface, limit upload speed
}
stop() {
# Stop the bandwidth shaping.
$TC qdisc del dev $IF root
}
restart() {
# Self-explanatory.
stop
sleep 1
start
}
show() {
# Display status of traffic control status.
$TC -s qdisc ls dev $IF
}
case "$1" in
start)
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
echo "Bandwidth shaping status for $IF:"
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|show}"
;;
esac
exit 0
thanks