Advertisement
cesarzeta

trafico_red_iptables

Mar 23rd, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #!/bin/bash
  2. let n=0
  3. while true
  4. do
  5. let tx_bytes=0
  6. let tx_bytes_nuevos=0
  7. let tx_bytes_actuales=0
  8. trafico_in=""
  9. total_in=""
  10. let rx_bytes=0
  11. let rx_bytes_nuevos=0
  12. let rx_bytes_actuales=0
  13. trafico_out=""
  14. total_out=""
  15. if [ -f /tmp/tx_bytes ]
  16. then
  17. sudo rm /tmp/tx_bytes
  18. elif [ -f /tmp/rx_bytes ]
  19. then
  20. sudo rm /tmp/rx_bytes
  21. fi
  22. sudo iptables -L INPUT -n -v -x > /tmp/tx_bytes
  23. sudo sed -i '1,2d' /tmp/tx_bytes
  24. sudo sed -i 's/^ *//g' /tmp/tx_bytes
  25. sudo sed -i 's/ */;/g' /tmp/tx_bytes
  26. sudo iptables -L OUTPUT -n -v -x > /tmp/rx_bytes
  27. sudo sed -i '1,2d' /tmp/rx_bytes
  28. sudo sed -i 's/^ *//g' /tmp/rx_bytes
  29. sudo sed -i 's/ */;/g' /tmp/rx_bytes
  30. while read line
  31. do
  32. line=`echo $line | cut -d";" -f2`
  33. tx_bytes=$line
  34. let tx_bytes_actuales+=$tx_bytes
  35. done < /tmp/tx_bytes
  36.  
  37. if [ $tx_bytes_actuales -lt 1024 ]
  38. then
  39. total_in=$tx_bytes_actuales
  40. bajado_total=`echo "$total_in B"`
  41. fi
  42. if [[ $tx_bytes_actuales -ge 1024 && $tx_bytes_actuales -lt 1048576 ]]
  43. then
  44. total_in=`echo "scale=2; $tx_bytes_actuales/1024" | bc`
  45. bajado_total=`echo "$total_in Kb"`
  46. fi
  47. if [ $tx_bytes_actuales -ge 1048576 ]
  48. then
  49. total_in=`echo "scale=2; $tx_bytes_actuales/1048576" | bc`
  50. bajado_total=`echo "$total_in Mb"`
  51. fi
  52. if [ $n -eq 0 ]
  53. then
  54. tx_bytes_anteriores=$tx_bytes_actuales
  55. fi
  56. let tx_bytes_nuevos=$tx_bytes_actuales-$tx_bytes_anteriores
  57. tx_bytes_anteriores=$tx_bytes_actuales
  58. trafico_in=`echo "scale=2; $tx_bytes_nuevos/1024" | bc`
  59. trafico_in=`echo "scale=2; $trafico_in/(11/2)" | bc`
  60. entrando=`echo "$trafico_in Kb/s"`
  61. while read line
  62. do
  63. line=`echo $line | cut -d";" -f2`
  64. rx_bytes=$line
  65. let rx_bytes_actuales+=$rx_bytes
  66. done < /tmp/rx_bytes
  67. if [ $rx_bytes_actuales -lt 1024 ]
  68. then
  69. total_out=$rx_bytes_actuales
  70. subido_total=`echo "$total_out B"`
  71. fi
  72. if [[ $rx_bytes_actuales -ge 1024 && $rx_bytes_actuales -lt 1048576 ]]
  73. then
  74. total_out=`echo "scale=2; $rx_bytes_actuales/1024" | bc`
  75. subido_total=`echo "$total_out Kb"`
  76. fi
  77. if [ $rx_bytes_actuales -ge 1048576 ]
  78. then
  79. total_out=`echo "scale=2; $rx_bytes_actuales/1048576" | bc`
  80. subido_total=`echo "$total_out Mb"`
  81. fi
  82. if [ $n -eq 0 ]
  83. then
  84. rx_bytes_anteriores=$rx_bytes_actuales
  85. fi
  86. let rx_bytes_nuevos=$rx_bytes_actuales-$rx_bytes_anteriores
  87. rx_bytes_anteriores=$rx_bytes_actuales
  88. trafico_out=`echo "scale=2; $rx_bytes_nuevos/1024" | bc`
  89. trafico_out=`echo "scale=2; $trafico_out/(11/2)" | bc`
  90. saliendo=`echo "$trafico_out Kb/s"`
  91. if [ -f /tmp/tx_rx_bytes ]
  92. then
  93. sed -i '1d' /tmp/tx_rx_bytes
  94. echo "$bajado_total:$entrando:$subido_total:$saliendo" > /tmp/tx_rx_bytes
  95. else
  96. echo "$bajado_total:$entrando:$subido_total:$saliendo" > /tmp/tx_rx_bytes
  97. fi
  98. let n=$n+1
  99. sleep 5
  100. done
  101. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement