Permalink
Browse files

netspeed script

  • Loading branch information...
1 parent 07fd349 commit 040f7c719c5fb4512ffbdc19e27e1f0c056bf9eb @housemaister committed Jul 19, 2013
Showing with 50 additions and 0 deletions.
  1. +50 −0 net/netspeed
View
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+raw=0
+
+while getopts ":ri:" opt; do
+ case $opt in
+ r)
+ raw=1
+ ;;
+ i)
+ IF=$OPTARG
+ ;;
+ \?)
+ echo "Invalid option: -$OPTARG" >&2
+ exit 1
+ ;;
+ :)
+ echo "Option -$OPTARG requires an argument." >&2
+ exit 1
+ ;;
+ esac
+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
+ exit 1
+fi
+
+while true
+do
+ R1=`cat /sys/class/net/$IF/statistics/rx_bytes`
+ T1=`cat /sys/class/net/$IF/statistics/tx_bytes`
+ 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`
+ if [ "$raw" -eq 1 ]; then
+ echo -e "$timestamp $TBPS $RBPS"
+ else
+ echo -e "[$timestamp]\ttx $IF: $TBPS b/s \trx $IF: $RBPS b/s"
+ fi
+done
+

0 comments on commit 040f7c7

Please sign in to comment.