Monitor linux traffic for shell and linux traffic for shell

Source: Internet
Author: User

Monitor linux traffic for shell and linux traffic for shell

If you want to view linux traffic in real time and do not want to go to a third-party tool, you can directly write and run it!

System: centos 6.5

Principle: Get the traffic from/proc/net/dev, and then get the traffic unit M by converting and dividing by the interval.

# ------------------ # Author: Administrator # Created: 2015-04-10 #------------------#! /Bin/bash # usage () {echo "useage: $0 ethname intervel" exit 0} # configure the network port if [$ #-gt 1]; then ethname = $1 num = 'ifconfig | grep $ {ethname} | wc-l' if [$ num-gt 0]; then ethname = $1 else echo "Can't find $ ethname, Please Check" exit 1 fifi # Time configuration intervel = 1if [$ #-gt 0]; then interval = $2 test = 'expr "$ interval" \ * 0' if ["$ test "! = "0"]; then echo "Invalid <interval> param' $ interval '. it shoshould be a integer number. usage: $0 ethname intervel "exit-1 fi if [$ interval-lt 1]; then echo" Invalid <interval> param '$ interval '. it shoshould be> 0 (seconds ). usage: $0 ethname intervel "exit-1 fifi # function to determine if [$ #-ne 2] then usagefiecho" $0 is starting, Device is $1, intervel is $2 "echo-e" \ n "typeset Rev old_Reveth = $1 intervel = $ 2old_Rev = 'cat/proc/net/dev | grep $ eth | awk '{print $1} '| awk-F: '{print $2}' 'Old _ Send = 'cat/proc/net/dev | grep $ eth | awk '{print $9}' # echo "old_Rev is $ old_Rev "# echo" Old_send is $ old_Send "while true do sleep $ intervel Rev = 'cat/proc/net/dev | grep $ eth | awk '{print $1}' | awk-F: '{print $2} ''Send = 'cat/proc/net/dev | grep $ eth | awk' {print $9}'' # echo "Rev is $ Rev" # echo "Send is $ Send" diff_1 = 'awk-v Rev = $ Rev-v old_Rev = $ old_Rev-v intervel = $ intervel 'in in {printf "Receive is % 6.2f", (Rev-old_Rev) /1024/1024/intervel * 8} ''diff_2 = 'awk-v Send = $ Send-v old_Send = $ old_Send-v intervel = $ intervel 'begin {printf" Send is % 6.2f ", (Send-old_Send)/1024/1024/intervel * 8} 'echo "'date + '% Y-% m-% d % H: % M: % s' Speed: IN $ {diff_1} mbps OUT $ {diff_2} mbps "old_Rev = $ Rev old_Send = $ Senddone

 

If you have any questions, contact:

362299908@qq.com

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.