Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


# Name of the traffic control command.

# 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= # 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.
    sleep 1


show() {

    # Display status of traffic control status.
    $TC -s qdisc ls dev $IF


case "$1" in


        echo -n "Starting bandwidth shaping: "
        echo "done"


        echo -n "Stopping bandwidth shaping: "
        echo "done"


        echo -n "Restarting bandwidth shaping: "
        echo "done"


        echo "Bandwidth shaping status for $IF:"
        echo ""


        echo "Usage: tc.bash {start|stop|restart|show}"


exit 0


share|improve this question
@Dennis Williamson: what did you do to get the code to format correctly? – Iain Mar 2 '11 at 21:48
@Iain: The markdown on this site doesn't like code that immediately follows a numbered list so I inserted a --- hrule between them. Anything would have worked though. – Dennis Williamson Mar 2 '11 at 22:12
i just copied and paste it from notepad – Craig-Aaron Mar 2 '11 at 22:23
@Dennis Williamson: Cheers - I've not encountered that before. – Iain Mar 2 '11 at 22:36
This looks suspiciously like homework, to me... Could you reword/expand your question? I don't understand what you mean by 'filter' in part "1" and I don't understand what you mean by 'do' in part "2", and I don't understand what you mean by 'control' in part "3". – JeffG Mar 2 '11 at 22:47

If they have not been renamed, you can find all ethernet devices by looking for directories matching eth* in /sys/class/net/. For applying these operations to multiple NICs and IP addresses, look into for loops. Here is an example



for I in /sys/class/net/eth*  
    I=$(basename $I)  
    for A in $ADDRESSES  
        echo $I $A  

On a system with two ethernet devices, this would output


share|improve this answer
i would apply it like this?, Addresses=" -" or can i use for INT in /sys/class/net/eth* do INT=$(basename $INT) for A in $adresses do echo $int %A done done – Craig-Aaron Mar 2 '11 at 22:19
You need to spend more time learning bash shell scripting and the tc command syntax. I'm not convinced that you've made a good faith effort to do this yet, and it's not helpful for you to receive code you don't understand. – sciurus Mar 3 '11 at 4:10

Get a list of ethernet links:

/sbin/ip link

Get a list of connected ethernet links:

/sbin/ip link | grep 'UP'

I cannot understand the rest of your question.

share|improve this answer
Hi Jeff, i need the script to work with different interfaces, and ip ranges, this is aimed at a corparate network so e.g network or ethernet port 1 is the external port and the internal port is ethernet 2. is that easy to incorporate in my code? – Craig-Aaron Mar 2 '11 at 22:57
With /bin/sh and /bin/bash you can access parameters given to the script via the $1 $2 $3 ... $n variables so you could write a bash script that takes your addresses as arguments. If you want to get fancy, checkout the bash getopts built-in – JeffG Mar 3 '11 at 1:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.