Skip to content

Instantly share code, notes, and snippets.

@rdkls
Last active December 24, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdkls/6822643 to your computer and use it in GitHub Desktop.
Save rdkls/6822643 to your computer and use it in GitHub Desktop.
Shell script to display an interface's bandwidth usage (Tx/Rx) every second
#!/bin/bash
secs=1
interface=eth0
echo "Displaying bandwidth on $interface every $secs seconds, ctrl-c to stop"
rx_bytes_prev=`ifconfig eth1|grep 'RX bytes'| sed -n 's/^\s*RX bytes:\([0-9]*\).*/\1/p'`
tx_bytes_prev=`ifconfig eth1|grep 'TX bytes'| sed -n 's/.*TX bytes:\([0-9]*\).*/\1/p'`
while [ true ] ; do
sleep $secs
rx_bytes=`ifconfig eth1|grep 'RX bytes'| sed -n 's/^\s*RX bytes:\([0-9]*\).*/\1/p'`
tx_bytes=`ifconfig eth1|grep 'TX bytes'| sed -n 's/.*TX bytes:\([0-9]*\).*/\1/p'`
rx_mbps=`echo "scale=2;($rx_bytes - $rx_bytes_prev) * 8 / 1000 / 1000" | bc`
tx_mbps=`echo "scale=2;($tx_bytes - $tx_bytes_prev) * 8 / 1000 / 1000" | bc`
echo "Rx $rx_mbps Mbps - Tx $tx_mbps Mbps"
tx_bytes_prev=$tx_bytes
rx_bytes_prev=$rx_bytes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment