Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

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
1  
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

2 Answers 2

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

#!/bin/bash  

ADDRESSES="192.0.2.1 192.0.2.2"  

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

On a system with two ethernet devices, this would output

eth0 192.0.2.1
eth0 192.0.2.2
eth1 192.0.2.1
eth1 192.0.2.2

share|improve this answer
    
i would apply it like this?, Addresses="192.168.1.1 - 192.168.1.254" or can i use 192.168.1.0 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 192.168.1.0 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

 
discard

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.