How to use Shell script to view real-time traffic of network card
This article introduces the knowledge of "how to use Shell script to view the real-time traffic of network card". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The code is as follows:
#! / 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 +% KRV% M:% S` TX"
RX=$ ((${RXnext}-${RXpre}))
TX=$ ((${TXnext}-${TXpre}))
If [$RX-lt 1024]; then
RX= "${RX} Bamp s"
Elif [$RX-gt 1048576]; then
RX=$ (echo $RX | awk'{print $1max 1048576 "MB/s"}')
Else
RX=$ (echo $RX | awk'{print $1amp 1024 "KB/s"}')
Fi
If [$TX-lt 1024]; then
TX= "${TX} Bamp s"
Elif [$TX-gt 1048576]; then
TX=$ (echo $TX | awk'{print $1max 1048576 "MB/s"}')
Else
TX=$ (echo $TX | awk'{print $1amp 1024 "KB/s"}')
Fi
Echo-e "$eth\ t $RX $TX"
Done
Call method:
The code is as follows:
. / traff.sh eth2
Or
Bash traff.sh eth2
The network card traffic can be displayed in real time:
This is the end of the content of "how to use Shell script to view network card real-time traffic". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!