Linux network bandwidth utilization
Today I’m going to share a script that I created as I found myself to be slightly paranoid and slightly like a kid. So let me explain why (if you don’t care you can skip to “The goal” part).
In general, I don’t like to install any third-party application unless it is really needed. Everyone who has been working in IT Operations knows what it means to add a new application to an environment. It brings several aspects along you need to cope with. Those mainly involve regular maintenance in terms of applying regular application fixes and security patches. And on top of, you have additional application in the system which is always a potential troublemaker (no matter how good can be the code of the application but it can possibly trigger some bug in other dependent application). The security aspect here is the thing that makes me be a bit paranoid.
On the other hand, I like to explore new possibilities I was not aware of so far. And there is of course the endless amount of such things. This supports to unleash the kid in me.
I believe now everyone understands why I didn’t choose any of the available applications on the internet to do the job. Especially nowadays when security is the topic of the day. And at the end of the day why install any other tool when Linux distributions already come with a bunch of features built-in.
The goal
So what I wanted to achieve? Basically, I like when I can see fancy stuff like resource utilization in percentage. But I was not able to find such a feature for the network. There are several tools that can show you the number of packets or bytes transferred but nothing showing you a nice percentage of network utilization. So I did a small research and came across the nice property which is class in sys folder. In /sys/class/ you can find a bunch of interesting things and of them is also for network interfaces.
Basically in /sys/class/net/ you can find all available network interfaces. If you enter the particular folder of an interface you can then read information about traffic, network speed, and so on.
So long story short here is the script I have made. It lists the available network interfaces while excluding loopback. Then it captures bytes transferred at the time and then after the defined time (EVERY variable). Below is the screenshot of how looks when it runs and what information it will provide to you.

#!/bin/bash# Author: Peter Prostredny (prostrednyp@gmail.com)
# Version: 0.1
# History: 28july2021 0.1 - Script for measuring NIC bandwidth createdif [ $# -ne 1 ]; then
echo "Your command line contains $# arguments."
echo "You must provide ONE ethernet interface name.";
echo Available interfaces: $(ls -I "lo" /sys/class/net/)
echo "Example: ./$(basename "$0")" ens3
exit 1;
fi
clear
EVERY=3
echo The page will give you numbers after first refresh so in $EVERY s.
NIC=$1
SPEED=$(cat /sys/class/net/$NIC/speed)
if [ $SPEED -lt 0 ]
then
SPEED=$(ifconfig $NIC | grep -oP 'txqueuelen \K\w+')
fiwhile true
do
RX_Bytes=$(cat /sys/class/net/$NIC/statistics/rx_bytes)
TX_Bytes=$(cat /sys/class/net/$NIC/statistics/tx_bytes)
sleep $EVERY
clear
RX_Bytes_5=$(cat /sys/class/net/$NIC/statistics/rx_bytes)
TX_Bytes_5=$(cat /sys/class/net/$NIC/statistics/tx_bytes)
#echo RX: $RX_Bytes
#echo RX_5: $RX_Bytes_5
#echo TX: $TX_Bytes
#echo TX_5: $TX_Bytes_5RX_Bytes_DIFF=$((RX_Bytes_5 - RX_Bytes))
TX_Bytes_DIFF=$((TX_Bytes_5 - TX_Bytes))
TOTAL_BC=$(echo "scale=4; ($RX_Bytes_DIFF + $TX_Bytes_DIFF) / 1048576 " | bc)
echo Bandwidth Speed: $SPEED Mbit/s Utilization: $(echo "scale=4; (($TOTAL_BC * 8) / $SPEED) * 100 " | bc)%
echo RX in $EVERY s: $RX_Bytes_DIFF Bytes
echo TX in $EVERY s: $TX_Bytes_DIFF Bytes
echo Total in $EVERY s: $TOTAL_BC MBytesecho ""
echo "Press [CTRL+C] to stop.."
done

The script uses properties that I believe are part of most of the Linux distributions. Anyway, I did a test only on Oracle Linux 7.9 . That means it should work also on other similar distributions like Red Hat, CentOS, and so on. But I hope that it will work also on other distros.