博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[shell]判断网络情况并加上时间戳
阅读量:7136 次
发布时间:2019-06-28

本文共 1590 字,大约阅读时间需要 5 分钟。

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

#!/bin/bashhost=$1if [ -z $host ]; then    echo "Usage: `basename $0` [HOST]"    exit 1fiwhile :; 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 \033[0;31mdown\033[0m"    else         echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $host is \033[0;32mok\033[0m -`echo $result | cut -d ':' -f 2`"        sleep 1 # avoid ping rain    fidone

 

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%%\n", 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%%\n", 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 spaceHost="www.baidu.com"# no ping requestCOUNT=1      #=== Local vars (do not change them)# Cron-friendly: Automaticaly change directory to the current onecd $(dirname "$0")      # Current script filenameSCRIPTNAME=$(basename "$0")      # Current date and timetoday=$(date '+%Y-%m-%d')currtime=$(date '+%H:%M:%S')      #=== Main scriptwhile :do    msg=$(ping -c $COUNT $Host | grep 'loss')    echo -e "`date +'%Y/%m/%d %H:%M:%S'` ($Host 64Bytes) $msg" #>> plwatch.txtdone

 

结果

 

转载地址:http://nvvrl.baihongyu.com/

你可能感兴趣的文章
phpcms二级菜单
查看>>
(4)pyspark---dataframe清理
查看>>
JS、PHP验证URL
查看>>
通过iscsi协议使用ceph rbd
查看>>
javascript 和 CoffeeScript 里的类
查看>>
POJ2239 Selecting Courses(二分图)
查看>>
最优二叉搜索树
查看>>
nginx php-cgi php
查看>>
ZetCode PyQt4 tutorial custom widget
查看>>
追求代码质量: 驯服复杂的冗长代码
查看>>
移动端页面适配ipad?
查看>>
js闲记
查看>>
CSS布局部分知识总结
查看>>
Jquery DataTables相关示例
查看>>
HihoCoder第三周与POJ2406:KMP算法总结
查看>>
利用python+seleniumUI自动化登录获取cookie后再去测试接口,今天终于搞定了
查看>>
《自动化技术中的进给电气传动》读书笔记一
查看>>
【转】UGUI实现unity摇杆
查看>>
setjmp与longjmp非局部跳转函数的使用
查看>>
jqgrid 加入右键菜单按钮管理
查看>>