Is there any way to calculate throughput per interface (e.g. eth0) in Linux system using ip command or ifconfig?
I tried bmon, nload - but is it possible to get the same result using some basic Linux command or basic commands in a script?
3 Answers
This other answer mentions /proc/net/dev. Below is an example script that roughly calculates "instantaneous" Bps by reading the file twice, (about) 1 second apart, then subtracting:
#!/bin/sh
[ "$#" -eq 1 ] || { printf 'usage: %s interface\n' "$0" >&2; exit 1; }
getstats () { grep "$1": /proc/net/dev
}
{ getstats "$1"; sleep 1; getstats "$1"; } \
| awk '{rcv=$2-rcv; trnsmt=$10-trnsmt} END {print rcv" "trnsmt}'Example usage (assuming the script is saved as ./showband):
$ ./showband eth0 # the result appears after about 1 second
1272439 535768
$The numbers are incoming_bytes_per_second outgoing_bytes_per_second.
Tested on Debian 9.
1You can use the contents of /proc/net/dev:
- one line per interface
- first line is a header that explains what the columns are
why not use iftop?
sudo yum install iftop
sudo iftop -i eth0