LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2011, 03:22 PM   #1
CraigAaron
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
Bash Scripting for TC


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?

#!/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
 
Old 03-02-2011, 03:35 PM   #2
kuyalfinator
Member
 
Registered: Jul 2003
Location: Los Angeles
Distribution: RedHat8 2.4.18-14 RedHat9 2.4.20-8 FreeBSD 5.2.1 Ubuntu 10_4 Mandrake 2.4.21-0.13mdk
Posts: 118

Rep: Reputation: 16
Quote:
Originally Posted by CraigAaron View Post
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?
I first like to run simple commands then try narrow it down to my needs. Your first question is finding active internet ports:

$/sbin/ifconfig
$man ifconfig <- useful for find out correct syntax to use

Your second question is how to get a range of IP Address. This is one is tricky because if you just want to know the ports ip address, then we use the command above with a pipe | and grep.

$/sbin/ifconfig | grep inet

Let me know if this is what your looking for.
 
Old 03-02-2011, 03:56 PM   #3
CraigAaron
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Original Poster
Rep: Reputation: 0
i have changed my mind with the ethernetports, how do i add in to this for the ethernet ports? so a array of some sort?

then seperate code for each tc i want to do?
e.g eth0=if1, eth1=if2
then do something

the range will be known ranges like 192.168.1.0/24 or 192.168.2.0.

sorry if i have confused you more
 
Old 03-02-2011, 06:24 PM   #4
kuyalfinator
Member
 
Registered: Jul 2003
Location: Los Angeles
Distribution: RedHat8 2.4.18-14 RedHat9 2.4.20-8 FreeBSD 5.2.1 Ubuntu 10_4 Mandrake 2.4.21-0.13mdk
Posts: 118

Rep: Reputation: 16
When I don't know what command to use to look for ports, try the manual.

$man -k port

or

$man -k ports

The man command is the greatest tool I use when I forget what syntax used.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting rpcaldeira Programming 11 10-09-2010 02:23 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash scripting help arturhawkwing Linux - General 1 08-10-2006 11:54 AM
Bash scripting vinoth.ilango Solaris / OpenSolaris 6 10-29-2004 04:41 AM
BASH If-then-else Scripting Help xianzai Programming 4 10-29-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration