Monitoring network traffic in Linux: Difference between revisions
From Leo's Notes
This page was last edited on 30 December 2021, at 01:41.
mNo edit summary |
m Leo moved page Network Traffic Monitoring to Monitoring network traffic in Linux: Better name for this page |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 30: | Line 30: | ||
LINE=`grep $1 /proc/net/dev`; | LINE=`grep $1 /proc/net/dev`; | ||
Rxn=`echo $LINE {{!}} awk '{print $2}'` | Rxn=`echo $LINE {{!}} awk '{print $2}'` | ||
Txn=`echo $LINE {{!}} awk '{print $ | Txn=`echo $LINE {{!}} awk '{print $10}'` | ||
if [ $Rx -ge 0 ] ; then | if [ $Rx -ge 0 ] ; then | ||
| Line 48: | Line 48: | ||
| lang = bash | | lang = bash | ||
}} | }} | ||
{{Navbox Linux}} | |||
[[Category:Linux]] | |||
Latest revision as of 01:41, 30 December 2021
On a bare system without nice tools like vnstat, use the following script to monitor your network usage on a Linux box.
#!/bin/bash
Delay=1
Device=$1
if [ -z "$Device" ] || ! grep -qw $Device /proc/net/dev ; then
echo "Usage: $0 interface"
echo "Error: Invalid device '$Device'."
grep ":" /proc/net/dev | awk -F: '{print $1}' | sed s@\ @@g
exit 1
fi
function format (){
if [ $1 -gt 1048576 ] ; then
echo $(( $1 / 1024 / 1024 )) "MB/s"
elif [ $1 -gt 1024 ] ; then
echo $(( $1 / 1024 )) "KB/s"
else
echo $1 "B/s"
fi
}
Rx=-1
Tx=-1
while true; do
LINE=`grep $1 /proc/net/dev`;
Rxn=`echo $LINE | awk '{print $2}'`
Txn=`echo $LINE | awk '{print $10}'`
if [ $Rx -ge 0 ] ; then
Rx_b=$((($Rxn - $Rx) / $Delay))
Tx_b=$((($Txn - $Tx) / $Delay))
Sum_b=$(($Rx_b+$Tx_b))
printf "In: %18s | Out: %18s | Total: %18s\n" "`format $Rx_b`" "`format $Tx_b`" "`format $Sum_b`"
fi
Rx=$Rxn
Tx=$Txn
sleep $Delay
done