BASH   72

PPS sh

Guest on 22nd September 2022 01:24:27 PM

  1. #!/bin/bash
  2. #
  3. # Script prints packets per second passing through default interface
  4. #
  5.  
  6. time="1"     # one second
  7. # Finding out default interface:
  8. def=`ip route | grep default`
  9. found=0
  10. for i in $def; do
  11.   if [ $found -eq 1 ]; then int=$i; break; fi;
  12.   if [ "$i" == "dev" ]; then found=1; fi ;
  13. done
  14.  
  15. while true
  16. do
  17.         txpkts_old="`cat /sys/class/net/$int/statistics/tx_packets`" # sent packets
  18.         rxpkts_old="`cat /sys/class/net/$int/statistics/rx_packets`" # recv packets
  19.         sleep $time
  20.         txpkts_new="`cat /sys/class/net/$int/statistics/tx_packets`" # sent packets
  21.         rxpkts_new="`cat /sys/class/net/$int/statistics/rx_packets`" # recv packets
  22.         txpkts="`expr $txpkts_new - $txpkts_old`"                    # evaluate expressions for sent packets
  23.         rxpkts="`expr $rxpkts_new - $rxpkts_old`"                    # evaluate expressions for recv packets
  24.         echo "tx $txpkts pkts/s - rx $rxpkts pkts/ on interface $int"
  25. done

Raw Paste


Login or Register to edit or fork this paste. It's free.