Advertisement
Guest User

Untitled

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