#!/bin/ksh
# Korn shell script
# Written by Jeff Turner - Context-Switch Limited (c) 2002
#
# Script name: netbytes
# Version: A.1
# Creation Date: 25th February, 2002

# Purpose:
# This script uses the kstat utility to determine input and output bytes 
# (per time-interval) for all network ports on a Solaris system

# Before the script starts producing output, clear the screen
clear

# Step 1: Determine the current network port(s) on the system
typeset -L PORTVAR=""
PORTVAR=$(ifconfig -a | nawk -F: '/^[a-z][^:]*:/ && $1 !~ /lo[0-9]/ { print $1 }'\
 | sed 's/[0-9]*$//' | sort -u)

# Step 2 - Display details for the port(s)
integer COUNT=5   SEC=1	# create variables for count and delay-times

echo "The following output contains the input and output bytes
(rbyes & obytes, respectively) for the system network ports.

These samples are taken using the 'kstat' utility with
a ${SEC}-second frequency between each sample.
There are ${COUNT} samples taken.

***NOTE***
Remember that the 'kstat' utility can take some time to read and
evaluate data before it is output. Therefore, the total elapsed 
time for the sampling may be more than ${COUNT} times ${SEC} second(s)!

To determine the total (in bytes) network traffic received or sent
in the total elapsed sampling time, calculate the difference between
rbytes or obytes for the first and last samples.\n\n"

# Now start the sampling of each network port (in turn)
set -- $PORTVAR
while (( $# >= 1 ))
do
echo "Started sampling at: $(date +'%T on %D') \n"
kstat -T u -m $1 ${SEC} ${COUNT} | egrep '(^name:|obytes[^0-9]|rbytes[^0-9])'
echo "\nCompleted sample at: $(date +'%T on %D') \n"
shift
done
