Bash Script - Traffic Shaping

Posted by Craig-Aaron on Server Fault See other posts from Server Fault or by Craig-Aaron
Published on 2011-03-02T21:12:16Z Indexed on 2011/03/02 23:26 UTC
Read the original article Hit count: 546

Filed under:
|
|

hey all, I was wondering if you could have a look at my script and help me add a few things to it,

  1. How do I get it to find how many active ethernet ports I have? and how do I filter more than 1 ethernet port
  2. How I get this to do a range of IP address?
  3. 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

© Server Fault or respective owner

Related posts about bash

Related posts about bandwidth