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

#!/bin/bash

host=$

if [ -z $host ]; then
echo "Usage: `basename $0` [HOST]"
exit
fi while :; do
result=`ping -W -c $host | grep 'bytes from '`
if [ $? -gt ]; 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 # 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%%\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 space
Host="www.baidu.com"
# no ping request
COUNT= #=== 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

结果

[shell]判断网络情况并加上时间戳的更多相关文章

  1. shell判断网络主机存活

    判断网络主机存活企业面试题4:写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多) #!/bin/sh#[ -f /etc/init.d/functions ] & ...

  2. android 开发中判断网络是否连接的代码

    在android的开发中,尤其是与访问网络有关的开发,都要判断一下手机是否连接上了网络,下面是一个判断是否连接网络的嗲吗片段: package cn.com.karl.util; import com ...

  3. Android项目实战(五十三):判断网络连接是否为有线状态(tv项目适配)

    一般对于android手机,我们可以通过sdk提供的方法判断网络情况 /** * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2 * 自定义 * * @p ...

  4. Unix shell判断和比较

    1.  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of c ...

  5. shell判断和比较

    http://blog.chinaunix.net/uid-7553302-id-183648.html 1  shell 的$! ,$?, $$,$@ $n        $1 the first ...

  6. 网络版shell之网络编程练习篇--telnet服务端

    网络版shell之网络编程练习篇--telnet服务端   以前写过一个shell命令解释器,对与shell命令解释器的执行流程有了清晰的认识,这段时间学习网络编程,至于网络编程的细节以及知识点,已经 ...

  7. [Javascript]网页链接加上时间戳防止串用户

    最近客服来报,一批用户访问公司网站的时候,由于其网络环境有代理服务器,导致A用户看到B用户的信息,这是非常尴尬的事情.解决的方法也很容易,给网址加上时间戳就可以了,用JS就能实现. JS代码如下 // ...

  8. Android判断网络是否打开,并打开设置网络界面

    由于Android的SDK版本不同所以里面的API和设置方式也是有少量变化的,尤其是在Android 3.0 及后面的版本,UI和显示方式也发生了变化,现在就以打开网络设置为例,同大家分享一下: 1. ...

  9. Android开发(七)——判断网络状态

    项目中难免会出现使用网络的情况,使用网络前得进行网络判断,看网上的网友一般有多种实现版本. 第一种: // 是否有网络连接 public static boolean isNetworkConnect ...

随机推荐

  1. http1.1 协议响应方面参数

    HTTP1.1   提供了一个必须的Host字段,而且建立好一次连接之后可以重复使用.提高用户的上网体验. 响应信息 HTTP/1.1 200 OK                           ...

  2. js firstChild 、nextSibling、lastChild、previousSibling、parentNode

    nextSibling下一个兄弟节点 previousSibling上一个兄弟 parentNode父亲节点 <select><option value="zs" ...

  3. 老毛桃pe装机工具备份系统

    电脑故障可以说是难以避免的,误操作或者修改了哪个设置系统就莫名其妙崩溃了.这在日常使用当中并不鲜见,许多用户就会寻求备份系统方法.有没有好的一键备份系统教程可以参考呢?在本篇教程中,就容我跟大家讲讲怎 ...

  4. C#三种定时器

    三个定时器分别是 实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用. System.Windows.Forms.Timer 提供以指定的 ...

  5. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  6. python学习笔记——multiprocessing 多进程组件-队列Queue

    1 消息队列 1.1 基本语法 消息队列:multiprocessing.Queue,Queue是对进程安全的队列,可以使用Queue实现对进程之间的数据传输:还有一个重要作用是作为缓存使用. Que ...

  7. JMeter UI 启动时报错

    Problem After running jmeter.bat command under Windows 7 with cmd  the following error is produced:W ...

  8. PLSQL_统计信息系列03_统计信息的收集

    20150506 Created By Baoxinjian

  9. hibernate validator工具类

    一.相关的gradle配置文件 // https://mvnrepository.com/artifact/javax.el/javax.el-api compile group: 'javax.el ...

  10. python 中 with 用法

    前言 with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源, 比如文件使用后自动关闭/线程中锁的自动获取和释放等. 问题引出 如下代码: fil ...