#! /bin/sh # set -x dirs="/bin /usr/bin /sbin /usr/sbin /etc" NETSTAT=`find ${dirs} -follow -name netstat -type f -print 2>/dev/null | head -n 1` IFCONFIG=`find ${dirs} -follow -name ifconfig -type f -print 2>/dev/null | head -n 1` if [ $# -gt 0 ] ; then if expr "${1}" + 0 2>&1 | grep non-numeric > /dev/null then echo "`basename ${0}`: non-numeric argument" exit 1 fi fi # Default interval is 10 seconds. interval=${1-10} # Get list of network interfaces if none was specified. ifaces=${2-`${NETSTAT} -in | grep "[0-9]" | cut -f1 -d" " | grep -v :`} ifaces=${2-`${IFCONFIG} -a | grep : | grep -v '^ ' | sed 's/:* .*$//'`} # See if `ifconfig' reports bytes. if ${IFCONFIG} `echo ${ifaces} | cut -d" " -f1` | grep -i bytes > /dev/null then unit=bytes else unit=packets fi # Digits subexpression for `sed' digits='\([0-9][0-9]*\)' case ${unit} in bytes) tab=' ' txrx() { ${IFCONFIG} "${1}" | grep bytes: | sed "s/[${tab} ]*RX bytes:${digits}.*TX bytes:${digits}.*/RX=\1 TX=\2/" } ;; packets) field() { fieldname=${1} val=0 set -f `${NETSTAT} -i | grep Name` while [ ! -z "${1}" ] do val=`expr ${val} + 1` if [ "${1}" = "${fieldname}" ] then break fi shift done echo ${val} } in=`field Ipkts` out=`field Opkts` unset field txrx() { ${NETSTAT} -in | grep "${1}" | sed "s/ */ /g" | cut -d" " -f${in},${out} | sed "s/^${digits} *${digits}.*$/RX=\1 TX=\2/" } ;; esac # Initialize trap for iface in ${ifaces} do trap="${trap} " done # So when we exit (via Ctrl-C), it leaves all the numbers up. trap "exit" 1 2 3 trap "printf \"${trap}\"" 0 # Cursor up 1 motion. cuu1=`tput cuu1` # Erase 1 line. el=`tput el` total_time=0 # Start a timer. sleep ${interval} & while : do for iface in ${ifaces} do iface_nm=`echo ${iface} | sed 's/:/_/'` eval `txrx ${iface}` # Get current count eval `echo TXC_${iface_nm}=${TX}` eval `echo RXC_${iface_nm}=${RX}` # Calculate data this interval. eval ` echo 'TXD_'${iface_nm}'=\` expr ${TXC_'${iface_nm}'} - ${TXP_'${iface_nm}'} 2>/dev/null \`' ` eval ` echo 'RXD_'${iface_nm}'=\` expr ${RXC_'${iface_nm}'} - ${RXP_'${iface_nm}'} 2>/dev/null \`' ` # Calculate total data. eval ` echo 'TXT_'${iface_nm}'=\` expr ${TXT_'${iface_nm}'-0} + ${TXD_'${iface_nm}':-0} \`' ` eval ` echo 'RXT_'${iface_nm}'=\` expr ${RXT_'${iface_nm}'-0} + ${RXD_'${iface_nm}':-0} \`' ` # Calculate current data rate (bandwidth). eval ` echo 'TXB_'${iface_nm}'=\` expr ${TXD_'${iface_nm}':-0} / ${interval} \`' ` eval ` echo 'RXB_'${iface_nm}'=\` expr ${RXD_'${iface_nm}':-0} / ${interval} \`' ` # Calculate average data rate. eval ` echo 'TXA_'${iface_nm}'=\` expr ${TXT_'${iface_nm}'} / ${total_time} 2>/dev/null \`' ` eval ` echo 'RXA_'${iface_nm}'=\` expr ${RXT_'${iface_nm}'} / ${total_time} 2>/dev/null \`' ` # Set new max bandwidth if reached. if eval [ ` echo '"${TXM_'${iface_nm}'--1}" -lt "${TXB_'${iface_nm}'-0}"' ` ] then eval `echo 'TXM_'${iface_nm}'=${TXB_'${iface_nm}'-0}'` fi if eval [ ` echo '"${RXM_'${iface_nm}'--1}" -lt "${RXB_'${iface_nm}'-0}"' ` ] then eval `echo 'RXM_'${iface_nm}'=${RXB_'${iface_nm}'-0}'` fi # Print the result. eval `echo printf \ '"%s%6s: TX=%s (%s max, %s avg) RX=%s (%s max, %s avg) ${unit}/sec"' \ "${el}" \ "${iface}"' \ "${TXB_'${iface_nm}'}" \ "${TXM_'${iface_nm}'}" \ "${TXA_'${iface_nm}'}" \ "${RXB_'${iface_nm}'}" \ "${RXM_'${iface_nm}'}" \ "${RXA_'${iface_nm}'}"'` echo # Set the current to previous. eval `echo 'TXP_'${iface_nm}'=${TXC_'${iface_nm}'}'` eval `echo 'RXP_'${iface_nm}'=${RXC_'${iface_nm}'}'` done # Move the cursor back up to the beginning. for iface in ${ifaces} do printf "%s\r" ${cuu1} done # Increment total time. total_time=`expr ${total_time} + ${interval}` # Wait for the timer to complete. wait # Start the next timer. sleep ${interval} & done