Linux View network traffic __linux

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.