usr bin env python import sys os if os getuid print Only root user can

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
import sys, os
if os.getuid() != 0:
print "Only root user can change inteface rate"
sys.exit(2)
if len(sys.argv) != 4:
print "Usage:\n%s <ethX> <ip_addr> <rate_in_mbits>\n\nexample: python %s eth0 213.8.154.201 10\n\nTo delete the rule, type:\n/sbin/tc qdisc del dev <ethX> root" % (sys.argv[0], sys.argv[0])
sys.exit(1)
device = sys.argv[1]
ip_addr = sys.argv[2]
rate = sys.argv[3]
os.system("/sbin/tc qdisc del dev %s root" % device)
os.system("/sbin/tc qdisc add dev %s root handle 1: cbq avpkt 1000 bandwidth 100mbit" % device)
os.system("/sbin/tc class add dev %s parent 1: classid 1:1 cbq rate %smbit allot 1500 prio 5 bounded isolated" % (device, rate))
os.system("/sbin/tc filter add dev %s parent 1: protocol ip prio 16 u32 match ip src %s flowid 1:1" % (device, ip_addr))
os.system("/sbin/tc qdisc add dev %s parent 1:1 sfq perturb 10" % device)