Write a shell script to count the local traffic during a certain time period

Source: Internet
Author: User


Below is the output information of my wlan0 NIC, you can use Ifconfig or Ifconfig | Sed-n '/wlan0/,10p ' gets the format of the displayed information

wlan0     Link encap:ethernet  hwaddr xx:xx:xx:xx:xx:xx  

          inet addr:172.29.164.251  bcast:172.29.167.255  mask:255.255.248.0

          INET6 addr:2001:da8:c803:4a57:e190:606f:3155:9369/64 Scope:global

          INET6 addr:fe80::6e71:d9ff:fe0b:27f6/64 scope:link

          INET6 addr:2001:da8:c803:4a57:6e71:d9ff:fe0b:27f6/64 Scope:global

Up broadcast RUNNING multicast mtu:1500 metric:1

RX packets:1305 errors:0 dropped:0 overruns:0 frame:0

TX packets:647 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:370741 (362.0 KiB) TX bytes:117872 (115.1 KiB)


The following script obtains the last line of Rx, TX information at two points in time, and then counts the total traffic over time.

The final printout statistics are on the screen.

#!/bin/bash

#network Traffic Statistics

#Write by Ricardo.su 2015-04-30


time_range=10


Time= ' Date +%m '-"%d" "%k": "%m"


rx_point_1= ' Ifconfig wlan0 | grep [[: Space:]]rx.*tx | awk ' {print $} ' | Cut-c7-'

tx_point_1= ' Ifconfig wlan0 | grep [[: Space:]]rx.*tx | awk ' {print $6} ' | Cut-c7-'


Sleep $TIME _range


Rx_point_2= ' Ifconfig wlan0 | grep [[: Space:]]rx.*tx | awk ' {print $} ' | Cut-c7-'

Tx_point_2= ' Ifconfig wlan0 | grep [[: Space:]]rx.*tx | awk ' {print $6} ' | Cut-c7-'


Rx_rs=$ (((rx_point_2-rx_point_1)/1024))

Tx_rs=$ (((tx_point_2-tx_point_1)/1024))

echo "Date $time"

echo "Average Network traffic in $TIME _range s:rx $rx _rs kb TX $tx _rs KB"



Write a shell script to count the local traffic over a period of time

Related Article

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.