zoukankan      html  css  js  c++  java
  • [shell]判断网络情况并加上时间戳

    最近需要做一个实时统计网络情况并统计误包率的脚本,下面是StackExchange上的一个剽窃,虽然不完全满足,但只可以输出一些信息

    #!/bin/bash
    
    host=$1
    
    if [ -z $host ]; then
        echo "Usage: `basename $0` [HOST]"
        exit 1
    fi
    
    while :; do
        result=`ping -W 1 -c 1 $host | grep 'bytes from '`
        if [ $? -gt 0 ]; then
            echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $host is 33[0;31mdown33[0m"
        else
             echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $host is 33[0;32mok33[0m -`echo $result | cut -d ':' -f 2`"
            sleep 1 # avoid ping rain
        fi
    done

    ping google.com | awk '{ sent=NR-1; received+=/^.*(time=.+ ms).*$/; loss=0; } { if (sent>0) loss=100-((received/sent)*100) } { print $0; printf "sent:%d received:%d loss:%d%%
    ", sent, received, loss; }'
    ping google.com | awk '{ sent=NR-1; received+=/^.*(time=.+ ms).*$/; loss=0; } { if (sent>0) loss=100-((received/sent)*100) } { printf "sent:%d received:%d loss:%d%%
    ", sent, received, loss }'
    ping ... | sed -n -e 's/.*(100% packet loss).*/1/p' 
                      -e 's_.*min/avg/max = [0-9]*/([0-9]*)/[0-9]*.*_1_p'

     统计每一帧的错误率

    #!/bin/bash
    #=== PARAMETERS change them here
    
    # add ip / hostname separated by while space
    Host="www.baidu.com"
    # no ping request
    COUNT=1
          
    #=== Local vars (do not change them)
    # Cron-friendly: Automaticaly change directory to the current one
    cd $(dirname "$0")
          
    # Current script filename
    SCRIPTNAME=$(basename "$0")
          
    # Current date and time
    today=$(date '+%Y-%m-%d')
    currtime=$(date '+%H:%M:%S')
          
    #=== Main script
    while :
    do
        msg=$(ping -c $COUNT $Host | grep 'loss')
        echo -e "`date +'%Y/%m/%d %H:%M:%S'` ($Host 64Bytes) $msg" #>> plwatch.txt
    done

    结果

  • 相关阅读:
    【FPGA】结构化层次化设计
    【FPGA】库、包和子程序 过程 函数
    【FPGA】8位奇偶校验 / 加(减)法器 VHDL程序
    【FPGA】顺序语句
    【FPGA】并行语句
    【FPGA】VHDL基础
    Luogu P3349 小星星
    Luogu P4284 概率充电器
    NOIP2018 | D2T2 & D2T3
    二项式反演 小记
  • 原文地址:https://www.cnblogs.com/aaronLinux/p/7074602.html
Copyright © 2011-2022 走看看