Linux View network traffic
Below the GUI, there is a lot of tools to show network traffic, so what to do under the command line.
It is obvious that there are, for example, Ifconfig, there will be such output:
RX bytes:1224128649 (1.1 GiB) TX bytes:34114947 (32.5 MiB)
After a while, the values change, and the difference between the two is the amount of traffic over time. However, this is too impersonal ...
The so-called do-it-yourself, ample clothing, we write a script, real-time display and refresh.
The script is as follows, or which sentence, I have limited skill, write the principle of scripting is good enough.
#!/bin/bash
If [-N "$"]; Then Eth_name=$1 Else Eth_name= "Eth0" Fi
I=0
send_o= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' recv_o= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' send_n= $send _o recv_n= $recv _o
While [$i-le 100000]; Todo Send_l= $send _n Recv_l= $recv _n Sleep 1 Send_n= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' Recv_n= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' i= ' expr $i + 1 ' send_r= ' expr $send _n-$send _l ' recv_r= ' expr $recv _n-$recv _l ' total_r= ' expr $send _r + $recv _r ' Send_ra= ' expr/($send _n-$send _o/)/$i ' Recv_ra= ' expr/($recv _n-$recv _o/)/$i ' Total_ra= ' expr $send _ra + $recv _ra ' Sendn= ' Ifconfig $eth _name | grep bytes | Awk-f/(' {print $} ' | awk-f/) ' {print $} ' Recvn= ' Ifconfig $eth _name | grep bytes | Awk-f/(' {print $} ' | awk-f/) ' {print $} ' Clear echo "Last Second:send rate: $send _r bytes/sec Recv rate: $RECV _r bytes/sec Total rate: $total _r bytes/sec" echo "Average value:send rate: $send _ra bytes/sec Recv rate: $RECV _ra bytes/sec Total rate: $total _ra bytes/sec" echo "Total traffic after Startup:send traffic: $sendn RECV traffic: $RECVN" Done |
The script (assuming the name traffic) shows eth0 traffic by default, and if you have more than one network card, pass it in as a parameter, such as:
./traffic eth1
The results of the operation are as follows: