Iftop.sh

Bash jimmyd 11 Views Size: 1.50 KB Posted on: Nov 25, 25 @ 2:26 PM
  1. 1#!/bin/sh
  2. 2
  3. 3if [ "$1" = "" ]; then
  4. 4 echo "Please provide one of the following interfaces as a parameter: (ie. ./iftop.sh eth0)"
  5. 5 grep -v lo /proc/net/dev | grep -v Inter | grep -v face | awk '{print $1}' | tr -d ':'
  6. 6 exit 1
  7. 7fi
  8. 8
  9. 9LASTRX=$(grep "$1" /proc/net/dev | awk '{print $2}')
  10. 10LASTTX=$(grep "$1" /proc/net/dev | awk '{print $10}')
  11. 11printf "%9s %12s %12s\n" "Interface" "Received" "Transmitted"
  12. 12echo "------------------------------------"
  13. 13LINES=0
  14. 14while :; do
  15. 15 sleep 1
  16. 16 LINES=$((LINES+1))
  17. 17 RX=$(grep "$1" /proc/net/dev | awk '{print $2}')
  18. 18 TX=$(grep "$1" /proc/net/dev | awk '{print $10}')
  19. 19
  20. 20 RXDIFF=$(($RX-$LASTRX))
  21. 21 TXDIFF=$(($TX-$LASTTX))
  22. 22
  23. 23 LASTRX=$RX
  24. 24 LASTTX=$TX
  25. 25
  26. 26 if [ $RXDIFF -gt $((1024*1024*1024)) ]; then
  27. 27 RXMOD="GB/s"
  28. 28 RXDIFF=$((RXDIFF/1024/1024/1024))
  29. 29 elif [ $RXDIFF -gt $((1024*12024)) ]; then
  30. 30 RXMOD="MB/s"
  31. 31 RXDIFF=$((RXDIFF/1024/1024))
  32. 32 elif [ $RXDIFF -gt 1024 ]; then
  33. 33 RXMOD="KB/s"
  34. 34 RXDIFF=$((RXDIFF/1024))
  35. 35 else
  36. 36 RXMOD="Bytes/s"
  37. 37 fi
  38. 38
  39. 39 if [ $TXDIFF -gt $((1024*1024*1024)) ]; then
  40. 40 TXMOD="GB/s"
  41. 41 TXDIFF=$((TXDIFF/1024/1024/1024))
  42. 42 elif [ $TXDIFF -gt $((1024*12024)) ]; then
  43. 43 TXMOD="MB/s"
  44. 44 TXDIFF=$((TXDIFF/1024/1024))
  45. 45 elif [ $TXDIFF -gt 1024 ]; then
  46. 46 TXMOD="KB/s"
  47. 47 TXDIFF=$((TXDIFF/1024))
  48. 48 else
  49. 49 TXMOD="Bytes/s"
  50. 50 fi
  51. 51
  52. 52 if [ $LINES -eq 20 ]; then
  53. 53 echo
  54. 54 printf "%9s %12s %12s\n" "int" "Received" "Transmitted"
  55. 55 echo "------------------------------------"
  56. 56 LINES=0
  57. 57 fi
  58. 58
  59. 59 printf "%9s %4s %7s %4s %7s\n" $1 $RXDIFF $RXMOD $TXDIFF $TXMOD
  60. 60
  61. 61done

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA), and — with your consent — to measure usage and show ads. See Privacy.