Chinaunix

标题: 如何获得带时间的ping的结果? [打印本页]

作者: killertip    时间: 2008-05-07 14:00
标题: 如何获得带时间的ping的结果?
有台服务器是断时续,想通过ping方式了解一下断网的规律,发现ping 的结果中没有系统时间,所以无法得知具体是什么时间容易断网,有什么办法可以同时记录系统时间和发包结果吗?
作者: springwind426    时间: 2008-05-07 20:05

  1. ping 127.0.0.1 | perl -ne 'use POSIX qw(strftime);print strftime("%Y-%m-%d %H:%M:%S", localtime),"\t",$_;'

  2. 或者

  3. ping 127.0.0.1 | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}'

复制代码

作者: icewiner    时间: 2008-05-08 10:37
  1. ping 127.0.0.1 | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}'
复制代码

支持

如果把这个脚本写到/bin/下面重命名为"pingt"

  1. [test@localhost ~]$ pingt 10.0.0.1
复制代码


怎样把ip地址传递到脚本中?
作者: icewiner    时间: 2008-05-08 10:55

  1. #!/usr/bin/env bash
  2. while getopts a: options
  3. do
  4.         case $options in
  5.                 a) ping $OPTARG | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}'
  6.         esac
  7. done
复制代码

..勉强实现了

  1. [test@localhost bin]$ pingt -a 192.168.1.1
  2. 2008-05-08 10:42:42     PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
  3. 2008-05-08 10:42:42     64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=2.65 ms
  4. 2008-05-08 10:42:43     64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.380 ms
  5. 2008-05-08 10:42:44     64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.867 ms
  6. 2008-05-08 10:42:45     64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.373 ms
复制代码


再问..
如何让"-a"成为默认参数以实现直接输入

  1. [test@localhost bin]$ pingt 192.168.1.1
复制代码

就可以运行?
作者: killertip    时间: 2008-05-09 14:40
还有一个菜问题,怎么样把输出结果到1.txt中去,用ping 127.0.0.1 >1.txt 可以,但用
ping 127.0.0.1 | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}' >1.txt就没有输出结果。
作者: daizhongxian    时间: 2008-05-09 15:06
,,,,,,,,,,,,,,,
作者: killertip    时间: 2008-05-09 15:11
原来用ping 127.0.0.1 | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}' >>1.txt就可以了,谢谢各位了。
作者: conall    时间: 2015-08-03 14:49
回复 2# springwind426
再请教一下,我的UNIX环境不支持gawk和strftime
> ping 127.0.0.1 | gawk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}'

CORRECT>ping 127.0.0.1 | awk '{print strftime("%Y-%m-%d %H:%M:%S") "\t" $0}' (y|n|e|a)? yes
%Y-%m-%d %H:%M:%S       127.0.0.1 is alive

改怎么办?
   
作者: raps    时间: 2017-06-20 22:03
回复 3# icewiner

多谢,正好用上




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2