編寫shell指令碼統計某個時間段內原生流量

來源:互聯網
上載者:User

標籤:時間段   流量   統計   網卡   資訊   shell指令碼   


下面是我的wlan0網卡的輸出資訊,你可以使用ifconfig 或者 ifconfig | sed -n ‘/wlan0/,10p‘ 擷取顯示的資訊的格式

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)


下面的指令碼通過擷取最後一行的RX、TX的資訊,在兩個時間點上擷取,然後統計這一段時間內的總流量。

最後列印輸出統計資訊在螢幕上。

#!/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 $2}‘ | 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 $2}‘ | 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"



編寫shell指令碼統計某個時間段內原生流量

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.