Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #!/bin/bash
  2. # tc uses the following units when passed as a parameter.
  3. # kbps: Kilobytes per second
  4. # mbps: Megabytes per second
  5. # kbit: Kilobits per second
  6. # mbit: Megabits per second
  7. # bps: Bytes per second
  8. # Amounts of data can be specified in:
  9. # kb or k: Kilobytes
  10. # mb or m: Megabytes
  11. # mbit: Megabits
  12. # kbit: Kilobits
  13. # To get the byte figure from bits, divide the number by 8 bit
  14. #
  15.  
  16. TC=/sbin/tc
  17. IF=eth0
  18.  
  19. # Download and Upload Bandwidth
  20. DNLD=10mbit
  21. UPLD=10mbit
  22. LATENCY=50ms
  23.  
  24. # Destination Host
  25. IP=128.2.213.24
  26.  
  27. # Filter options for limiting the intended interface.
  28. U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
  29.  
  30. start() {
  31. # We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
  32. # For detailed configuration options, please consult Linux man
  33. # page.
  34. $TC qdisc add dev $IF root handle 1: htb default 30
  35. $TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
  36. $TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
  37. $TC qdisc add dev $IF parent 1:1 handle 10: netem delay $LATENCY
  38. $TC qdisc add dev $IF parent 1:2 handle 20: netem delay $LATENCY
  39. $U32 match ip dst $IP/32 flowid 1:1
  40. $U32 match ip src $IP/32 flowid 1:2
  41. # The first line creates the root qdisc, and the next two lines
  42. # create two child qdisc that are to be used to shape download
  43. # and upload bandwidth.
  44. #
  45. # The 4th and 5th line creates the filter to match the interface.
  46. # The 'dst' IP address is used to limit download speed, and the
  47. # 'src' IP address is used to limit upload speed.
  48. }
  49.  
  50. stop() {
  51. # Stop the bandwidth shaping.
  52. $TC qdisc del dev $IF root
  53. }
  54.  
  55. restart() {
  56. # Self-explanatory.
  57. stop
  58. sleep 1
  59. start
  60. }
  61.  
  62. show() {
  63. # Display status of traffic control status.
  64. $TC -s qdisc ls dev $IF
  65. }
  66.  
  67.  
  68. case "$1" in
  69. start)
  70. echo -n "Starting bandwidth shaping: "
  71. start
  72. echo "done"
  73. ;;
  74. stop)
  75. echo -n "Stopping bandwidth shaping: "
  76. stop
  77. echo "done"
  78. ;;
  79. restart)
  80. echo -n "Restarting bandwidth shaping: "
  81. if [ "$#" -ne 2 ]; then
  82. echo "Illegal number of parameters"
  83. echo "Usage: tc.bash restart bw(mbps)"
  84. exit 1
  85. fi
  86.  
  87. DNLD=$2mbit
  88. UPLD=$2mbit
  89. echo "Network throughput: $2 Mbps"
  90. restart
  91. echo "done"
  92. ;;
  93. show)
  94. echo "Bandwidth shaping status for $IF:"
  95. show
  96. echo ""
  97. ;;
  98. *)
  99. pwd=$(pwd)
  100. echo "Usage: tc.bash {start|stop|restart|show}"
  101. ;;
  102. esac
  103.  
  104. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement