Plesae Check this Script

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Plesae Check this Script

Post by mudasir »

AOA,

Few weeks back i posted a link to my shaper script in one of the posts regarding BANDWIDTH Restriction. My Script was very long and i was unable to post the whole script on LP Forums. I have made my script very short by using for loop, i am unable to test the script as i dont have any Computer on which Linux is installed. Please Check wheather this script will work on not

Code: Select all

 
#!/bin/sh 

### Local Network Interface  ###
IFACE="eth0"

### Rate in KiloBit, at which you want to limit  ###
CRATE="128kbit"

###  IP Range one
IP1="10.10.10" ###

### IP Range two ###
IP2="10.10.11"

if [ "$1" = "status" ]
then
	tc -s qdisc ls dev $IFACE
	tc -s class ls dev $IFACE
	exit
fi

echo Cleaning qdiscs if exists any

# clean existing down- and uplink qdiscs, hide errors
tc qdisc del dev $IFACE root 2> /dev/null > /dev/null

if [ "$1" = "stop" ] 
then 
	exit
fi

echo 
echo Now Setting $CRATE per user at Interface $IFACE

tc qdisc add dev $IFACE root handle 10: cbq bandwidth 10Mbit avpkt 1000
tc class add dev $IFACE parent 10:0 classid 10:1 cbq bandwidth 10Mbit rate 10Mbit allot 1514 weight 1Mbit prio 8 maxburst 20 avpkt 1000

for ((  i = 3 ;  i <= 509;  i++  ))
do
tc class add dev $IFACE parent 10:1 classid 10:$i cbq bandwidth 10Mbit rate $CRATE allot 1514 weight 1Kbit prio 5 maxburst 20 avpkt 1000 bounded
done

for ((  j = 2 ;  j <= 254;  j++  ))
do
	for ((  k = 3 ;  k <= 255;  k++  ))
	do
tc filter add dev $IFACE parent 10:0 protocol ip prio 100 u32 match ip dst $IP1.$j flowid 10:$k
done
done

for ((  l = 1 ;  l <= 254;  l++  ))
do
	for ((  m = 256 ;  m <= 509;  m++  ))
	do
tc filter add dev $IFACE parent 10:0 protocol ip prio 100 u32 match ip dst $IP2.$l flowid 10:$m
done
done
Please change the IP RANGE and CRATE according to your need, if you try to use it on your running setup.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
x2oxen
Major General
Posts: 1114
Joined: Wed Aug 22, 2007 3:17 pm
Location: Faisalabad
Contact:

Post by x2oxen »

i will check it for you but the last script was a good one because you can put different bandwidth rate on different ips in that!
Muhammad Usman
+92-321-6640501
Chemonics International
http://usmanpk.com
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Dear x2oxen,

If you know shell scripting then you can do that in this script as well, just have to end the for loop and start a new one, but that will be a bit confusing in this script as compared to the last one. You can do that by using IF THEN Else also.

That script is very very good regarding BANDWIDTH SHAPING.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

This script is not working, please dont try this, what it is doing is that it is giving each IP Address all 509 ID's. so in total it is creating 129795 lines....please dont try this...Now i am working to make this script work fine.

The error is in the nested for loops, both the nested loops are doing this..
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Here is the script that is working perfectly, i have tried it and it is working fine.

Code: Select all

#!/bin/sh

### Local Network Interface  ###
IFACE="eth0"

### Rate in KiloBit, at which you want to limit  ###
CRATE="128kbit"

###  IP Range one
IP1="10.10.10" ###

### IP Range two ###
IP2="10.10.11"

if [ "$1" = "status" ]
then
   tc -s qdisc ls dev $IFACE
   tc -s class ls dev $IFACE
   exit
fi

echo Cleaning qdiscs if exists any

# clean existing down- and uplink qdiscs, hide errors
tc qdisc del dev $IFACE root 2> /dev/null > /dev/null

if [ "$1" = "stop" ]
then
   exit
fi

echo
echo Now Setting $CRATE per user at Interface $IFACE

tc qdisc add dev $IFACE root handle 10: cbq bandwidth 10Mbit avpkt 1000
tc class add dev $IFACE parent 10:0 classid 10:1 cbq bandwidth 10Mbit rate 10Mbit allot 1514 weight 1Mbit prio 8 maxburst 20 avpkt 1000

for ((  i = 3 ;  i <= 509;  i++  ))
do
tc class add dev $IFACE parent 10:1 classid 10:$i cbq bandwidth 10Mbit rate $CRATE allot 1514 weight 1Kbit prio 5 maxburst 20 avpkt 1000 bounded
done

k=3
for ((  j = 2 ;  j <= 254;  j++  ))
do
tc filter add dev $IFACE parent 10:0 protocol ip prio 100 u32 match ip dst $IP1.$j flowid 10:$k
k=`expr $k + 1`
done

m=256
for ((  l = 1 ;  l <= 254;  l++  ))
do
tc filter add dev $IFACE parent 10:0 protocol ip prio 100 u32 match ip dst $IP2.$l flowid 10:$m
m=`expr $m + 1`
done
This script is working perfectly. Please Look at this one and tell me what else can i do to make this script more efficient.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
Post Reply