Skip to content

Instantly share code, notes, and snippets.

@rgo
Created December 23, 2011 18:42
Show Gist options
  • Save rgo/1515041 to your computer and use it in GitHub Desktop.
Save rgo/1515041 to your computer and use it in GitHub Desktop.
Calculates bandwith with netstat
#!/bin/bash
#
## Calomel.org calomel_interface_stats.sh
#
IF=eth0
SECS=`uptime | awk '{ if ($3 ~ /:/) { split($3,a,":"); print (a[1]*60+a[2])*60} else { split($3,b,":"); split($5,a,":"); print b[1]*86400+(a[1]*60+a[2])*60} }'`
EXT_IN=`netstat -n -i $IF | grep $IF | tail -1 | awk '{print $4}'`
EXT_OUT=`netstat -n -i $IF | grep $IF | tail -1 | awk '{print $8}'`
echo " "
echo "External interface bandwidth usage:"
echo " uptime " $(($SECS/86400)) "days"
echo " ExtIf in total " $(($EXT_IN/1000000000)) "GBytes"
echo " ExtIf out total " $(($EXT_OUT/1000000000)) "GBytes"
echo " ExtIf in/day " $(($EXT_IN*86400/$SECS/1000000)) "MBytes/day"
echo " ExtIf out/day " $(($EXT_OUT*86400/$SECS/1000000)) "MBytes/day"
echo " ExtIf in/30day " $(($EXT_IN*86400*30/$SECS/1000000000)) "GBytes/month"
echo " ExtIf out/30day " $(($EXT_OUT*86400*30/$SECS/1000000000)) "GBytes/month"
echo " ExtIf in+out/30day " $((($EXT_OUT+$EXT_IN)*86400*30/$SECS/1000000000)) "GBytes/month"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment