Skip to content

Instantly share code, notes, and snippets.

@yukinagae
Forked from stephenlb/network-rate.sh
Last active November 9, 2017 10:07
Show Gist options
  • Save yukinagae/0708dee1a3030c293eec41b96e0f99cd to your computer and use it in GitHub Desktop.
Save yukinagae/0708dee1a3030c293eec41b96e0f99cd to your computer and use it in GitHub Desktop.
Network Throughput Bandwidth Rate Consumption Linux Command
#!/bin/bash
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- USAGE -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
##
## ./network-rate.sh eth0
##
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
tx_bytes=0
rx_bytes=0
tx_bps=0
rx_bps=0
# update dev delay
update()
{
delay=$2
cat /proc/net/dev | grep $1 >/tmp/netdev_rate
#ts=`date +%s`
read dev rb rp re rd ri rf rc rm tb tp te td ti tc ta tc </tmp/netdev_rate
#echo $dev $rb $tb
if [ $tx_bytes -gt 0 ] ; then
tx_b=$(($tb - $tx_bytes))
tx_bps=$(($tx_b / $delay))
tx_kBps=$(($tx_bps / 1024))
tx_kbps=$(($tx_kBps * 8))
tx_mBps=$(($tx_bps / 1048576))
tx_mbps=$(($tx_mBps * 8))
rx_b=$(($rb - $rx_bytes))
rx_bps=$(($rx_b / $delay))
rx_kBps=$(($rx_bps / 1024))
rx_kbps=$(($rx_kBps * 8))
rx_mBps=$(($rx_bps / 1048576))
rx_mbps=$(($rx_mBps * 8))
fi
tx_bytes=$tb
rx_bytes=$rb
}
update $1 1
while true
do
sleep 1
update $1 1
echo "tx $tx_bytes rx $rx_bytes tx KBit/s $tx_kbps rx KBit/s $rx_kbps"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment