Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 49 additions and 14 deletions.
  1. +49 −14 net/netspeed
View
63 net/netspeed
@@ -1,12 +1,20 @@
#!/bin/bash
raw=0
+kb=0
+byte=0
-while getopts ":ri:" opt; do
+while getopts ":rkbi:" opt; do
case $opt in
r)
raw=1
;;
+ k)
+ kb=1
+ ;;
+ b)
+ byte=1
+ ;;
i)
IF=$OPTARG
;;
@@ -22,29 +30,56 @@ while getopts ":ri:" opt; do
done
if [ -z "$IF" ]; then
- echo $0: prints the tx/rx speed at given network interface in b/s
- echo usage: $0 [-r] -i network-interface
- echo e.g. $0 -i eth0
- echo -r outputs in raw format: timestamp tx rx
+ echo "$0: prints the tx/rx speed/bytes at given network interface in b/s"
+ echo " usage: $0 [-rkb] -i network-interface"
+ echo " e.g. $0 -i eth0"
+ echo " -k sets the unit to kbyte(/s)"
+ echo " -b outputs the total (k)byte; without the output is (k)byte/s"
+ echo " -r outputs in raw format: TIMESTAMP tx rx"
exit 1
fi
+R2=`cat /sys/class/net/$IF/statistics/rx_bytes`
+T2=`cat /sys/class/net/$IF/statistics/tx_bytes`
+TIMESTAMP=`date +%s.%N`
+
while true
do
- R1=`cat /sys/class/net/$IF/statistics/rx_bytes`
- T1=`cat /sys/class/net/$IF/statistics/tx_bytes`
+ R1=$R2
+ T1=$T2
+ OLDTIMESTAMP=$TIMESTAMP
sleep 1
R2=`cat /sys/class/net/$IF/statistics/rx_bytes`
T2=`cat /sys/class/net/$IF/statistics/tx_bytes`
- TBPS=`expr $T2 - $T1`
- RBPS=`expr $R2 - $R1`
- TKBPS=`expr $TBPS / 1024`
- RKBPS=`expr $RBPS / 1024`
- timestamp=`date +%s.%N`
+ TIMESTAMP=`date +%s.%N`
+ DELTAT=`expr $T2 - $T1`
+ DELTAR=`expr $R2 - $R1`
+ DELTATIME=`echo "scale=8; $TIMESTAMP - $OLDTIMESTAMP" | bc`
+ if [ "$byte" -eq 1 ]; then
+ if [ "$kb" -eq 1 ]; then
+ TSPEED=`expr $DELTAT / 1024`
+ RSPEED=`expr $DELTAR / 1024`
+ UNIT='kbyte'
+ else
+ TSPEED=$DELTAT
+ RSPEED=$DELTAR
+ UNIT='byte'
+ fi
+ else
+ if [ "$kb" -eq 1 ]; then
+ TSPEED=`echo "scale=0; $DELTAT / 1024 / $DELTATIME" | bc`
+ RSPEED=`echo "scale=0; $DELTAR / 1024 / $DELTATIME" | bc`
+ UNIT='kb/s'
+ else
+ TSPEED=`echo "scale=0; $DELTAT / $DELTATIME" | bc`
+ RSPEED=`echo "scale=0; $DELTAR / $DELTATIME" | bc`
+ UNIT='b/s'
+ fi
+ fi
if [ "$raw" -eq 1 ]; then
- echo -e "$timestamp $TBPS $RBPS"
+ echo -e "$TIMESTAMP $TSPEED $RSPEED"
else
- echo -e "[$timestamp]\ttx $IF: $TBPS b/s \trx $IF: $RBPS b/s"
+ echo -e "[$TIMESTAMP]\ttx $IF: $TSPEED $UNIT \trx $IF: $RSPEED $UNIT"
fi
done

No commit comments for this range

Something went wrong with that request. Please try again.