• 网络流量监控shell脚本


    网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可.

    #!/bin/bash

    #Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>
    #Usage2,print on terminal: netmonitor <MinKB> <Interval>
    #Record the value great than 100KB, 3 second Interval:
    #Example1,record: netmonitor 100 3 xxx.txt
    #Example2,print: netmonitor 100 3

    #参数说明
    #第一个是速度记录的下限,单位是KB,即超过多少KB才记录,0为全部记录
    #第二个参数是采样间隔,即隔多少秒记录一次
    #第三个参数是输出的文件名,为空的话就直接在终端打印

    #xieqianli
    #2011-4-25

    #同时修改下列变量可以固定参数,使用脚本时就无需参数
    MinKB="$1"
    Interval="$2"
    FileName="$3"
    NetDev="wlan0"

    #检查参数完整性
    if [ "$Interval" == "" ];then
    echo "Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>"
    echo "Usage2,print on terminal: netmonitor <MinKB> <Interval>"
    echo "Record the values great than 100KB, 3 seconds Interval:"
    echo "Example1,record: netmonitor 100 3 xxx.txt"
    echo "Example2,print: netmonitor 100 3"
    exit 1
    fi

    #检查 MinKB 参数的合法性
    check=`echo $MinKB | grep "[^0-9]"`
    if [ "$check" != "" ];then
    echo "$MinKB is not a number"
    exit 0
    fi

    len=${#MinKB}
    char1=`echo $MinKB | cut -c 1`
    if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
    echo "$MinKB is not a number"
    exit 0
    fi

    #检查 Interval 参数的合法性
    check=`echo $Interval | grep "[^0-9]"`
    if [ "$check" != "" ];then
    echo "$Interval is not a number"
    exit 0
    fi

    len=${#Interval}
    char1=`echo $Interval | cut -c 1`
    if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
    echo "$Interval is not a number"
    exit 0
    fi

    while [ 1 ]
    do
    #读取已接收和已发送字节数,间隔一秒
    REC1=`cat /proc/net/dev | grep $NetDev | sed 's/:/ /' | tr -s " " | cut -d " " -f 3`
    SEND1=`cat /proc/net/dev | grep $NetDev | sed 's/:/ /' | tr -s " " | cut -d " " -f 11`
    sleep $Interval
    REC2=`cat /proc/net/dev | grep $NetDev | sed 's/:/ /' | tr -s " " | cut -d " " -f 3`
    SEND2=`cat /proc/net/dev | grep $NetDev | sed 's/:/ /' | tr -s " " | cut -d " " -f 11`

    if [ $((REC2)) -lt $((REC1)) ];then
    exit 1
    fi

    if [ $((SEND2)) -lt $((SEND1)) ];then
    exit 1
    fi

    RESULT_REC=$((($REC2-$REC1)/$Interval))
    RESULT_SENT=$((($SEND2-$SEND1)/$Interval))

    #进行B,KB和MB的单位换算
    if [ $RESULT_REC -lt $((1024)) ]; then
    SPEED_REC="$RESULT_REC B "
    elif [ $RESULT_REC -gt $((1024)) -a $RESULT_REC -lt $((1048576)) ]; then
    SPEED_REC="$(($RESULT_REC/1024)) KB"
    elif [ $RESULT_REC -gt $((1048576)) ];then
    SPEED_REC="$(($RESULT_REC/1048576)) MB"
    fi

    if [ $RESULT_SENT -lt $((1024)) ]; then
    SPEED_SENT="$RESULT_SENT B "
    elif [ $RESULT_SENT -gt $((1024)) -a $RESULT_SENT -lt $((1048576)) ]; then
    SPEED_SENT="$(($RESULT_SENT/1024)) KB"
    elif [ $RESULT_SENT -gt $((1048576)) ];then
    SPEED_SENT="$(($RESULT_SENT/1048576)) MB"
    fi

    #求时间
    TIME=`date`

    #左边填充空格,把结果进行右对齐
    ZERO=$((8-${#SPEED_REC}))
    for (( i=0; i<$ZERO; i=i+1 ))
    do
    SPEED_REC=" $SPEED_REC"
    done

    ZERO=$((8-${#SPEED_SENT}))
    for (( i=0; i<$ZERO; i=i+1 ))
    do
    SPEED_SENT=" $SPEED_SENT"
    done

    REC_KB="$(($RESULT_REC/1024))"
    SEND_KB="$(($RESULT_SENT/1024))"

    #过滤掉小于 MinKB 的记录
    if [ "$MinKB" != "0" -a $((REC_KB)) -lt $MinKB -a $((SEND_KB)) -lt $MinKB ];then
    continue
    fi

    #输出结果
    if [ "$FileName" = "" ];then
    echo "$TIME Receive:$SPEED_REC Send:$SPEED_SENT"
    else
    echo "$TIME Receive:$SPEED_REC Send:$SPEED_SENT" >> $FileName
    fi
    done
  • 相关阅读:
    以下文件中的行尾不一致。要将行尾标准化吗
    用户 NT AUTHORITYNETWORK SERVICE 登录失败
    sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
    附加数据库对于服务器失败(Microsoft.SqlServer.Smo),无法升级数据库,因为它是只读的,或者具有只读文件
    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法
    HTTP 错误 404.2
    vs智能提示突然消失的解决办法 (vs2008 vs2010 vs2012 智能提示)
    Visual Studio 常用快捷键
    403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOLASP.NET v4.0"问题
    短信轰炸PC版
  • 原文地址:https://www.cnblogs.com/xieqianli/p/4219690.html
Copyright © 2020-2023  润新知