View linux network card traffic in real time

https://www.oschina.net/code/snippet_20912_2989

Save the following script as an executable script file, such as traffic.sh.

1. This script can customize the interface you want to view, accurate to decimals, and can display the unit flexibly according to the flow rate.
2. The collection interval of this script is 1 second.
3. This script does not require additional software installation. It can be dealt with in emergencies, such as temporarily wanting to see if there is traffic passing through, and about how much.
4. Due to the different calculation accuracy of some traffic viewing software, it is impossible to match the values ​​displayed by this script. The displayed results of this script are compared with du meter, and the difference is very small. Also, the transfer speed displayed by the transfer tool itself is not accurate.
Usage:
1. chmod +x ./traff.sh to change the file into an executable script.
2. ./traff.sh eth0 can start monitoring the interface eth0 traffic, press ctrl+c to exit.


#!/bin/bash
while [ "1" ]
do
eth = $ 1
RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
echo  -e  "\t RX `date +%k:%M:%S` TX"
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
 
if [[ $RX -lt 1024 ]];then
RX="${RX}B/s"
elif [[ $RX -gt 1048576 ]];then
RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
else
RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
be
 
if [[ $TX -lt 1024 ]];then
TX="${TX}B/s"
elif [[ $TX -gt 1048576 ]];then
TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
else
TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
be
 
echo -e "$eth \t $RX   $TX "
done

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326254267&siteId=291194637