如何为每个ping结果添加时间戳?

Joh*_*Mee 61 bash bsd ping osx-lion

Ping默认返回此值:

64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
Run Code Online (Sandbox Code Playgroud)

有什么方法可以让它添加时间戳吗?

例如,

Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
Run Code Online (Sandbox Code Playgroud)

我在OS X v10.7(Lion)上似乎有一些BSD版本的ping.

ric*_*chk 95

由于某种原因,我无法将基于Perl的解决方案重定向到文件,因此我一直在搜索并找到了bash唯一的方法:

ping www.google.fr | while read pong; do echo "$(date): $pong"; done

Wed Jun 26 13:09:23 CEST 2013: PING www.google.fr (173.194.40.56) 56(84) bytes of data.
Wed Jun 26 13:09:23 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=1 ttl=57 time=7.26 ms
Wed Jun 26 13:09:24 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=2 ttl=57 time=8.14 ms
Run Code Online (Sandbox Code Playgroud)

功劳归功于https://askubuntu.com/a/137246

  • 我实际上更喜欢这种方法,因为它不使用perl或awk. (6认同)
  • 为了看到超时,所需要的只是在管道(`|`)之前将`stderr`重定向到`stdout`,如下所示:`ping $ host 2>&1 | 读pong; 回声"$(date):$ pong"; done`.如果您希望将(或附加)写入文件,您可以重定向整个命令(完成后).另外,如果你不想生成子shell,`date`命令支持`echo`ing任意输入,如下所示:`ping $ host 2>&1 | 读pong; 做日期"+%c:$ pong"; done`.请注意,`date`的`format`参数(以`+`开头)可以随意定制.有关详细信息,请参阅`man date`. (2认同)

Pau*_*ce. 77

如果您的AWK没有strftime():

ping host | perl -nle 'print scalar(localtime), " ", $_'
Run Code Online (Sandbox Code Playgroud)

要将其重定向到文件,请使用标准shell重定向并关闭输出缓冲:

ping host | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > outputfile
Run Code Online (Sandbox Code Playgroud)

如果您想要ISO8601格式的时间戳:

ping host | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile
Run Code Online (Sandbox Code Playgroud)


ДМИ*_*КОВ 29

来自man ping:

   -D     Print timestamp (unix time + microseconds as in gettimeofday) before each line.
Run Code Online (Sandbox Code Playgroud)

它会产生这样的东西:

[1337577886.346622] 64 bytes from 4.2.2.2: icmp_req=1 ttl=243 time=47.1 ms
Run Code Online (Sandbox Code Playgroud)

然后可以从ping响应中解析出时间戳,并将其转换为所需的格式date.

  • 抱歉.adsl在我添加标签时退出了...它是OSX Lion - 没有"-D"选项:-( (2认同)

xua*_*eng 13

  1. 终端输出:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}'

  2. 文件输出:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt

  3. 终端+文件输出:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' | tee test.txt

  4. 文件输出背景:

    nohup ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt &


mar*_*rio 10

更简单的选项只是使用ts(1)moreutils(在大多数发行版上相当标准)。

$ ping 1.1.1.1 | ts 

Feb 13 12:49:17 PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data. 
Feb 13 12:49:17 64 bytes from 1.1.1.1: icmp_seq=1 ttl=57 time=5.92 ms
Feb 13 12:49:18 64 bytes from 1.1.1.1: icmp_seq=2 ttl=57 time=5.30 ms
Feb 13 12:49:19 64 bytes from 1.1.1.1: icmp_seq=3 ttl=57 time=5.71 ms
Feb 13 12:49:20 64 bytes from 1.1.1.1: icmp_seq=4 ttl=57 time=5.86 ms
Run Code Online (Sandbox Code Playgroud)

或者

 ping 1.1.1.1 -I eth0 | ts "[%FT%X]"
Run Code Online (Sandbox Code Playgroud)

允许使用与 shell/date解决方法相同的 strftime 格式字符串。


Bro*_*omo 8

我原来的提交不正确,因为它没有评估每一行的日期.已经进行了更正.

试试这个

 ping google.com | xargs -L 1 -I '{}' date '+%+: {}'
Run Code Online (Sandbox Code Playgroud)

产生以下输出

Thu Aug 15 10:13:59 PDT 2013: PING google.com (74.125.239.103): 56 data bytes
Thu Aug 15 10:13:59 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=0 ttl=55 time=14.983 ms
Thu Aug 15 10:14:00 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=1 ttl=55 time=17.340 ms
Thu Aug 15 10:14:01 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=2 ttl=55 time=15.898 ms
Thu Aug 15 10:14:02 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=3 ttl=55 time=15.720 ms
Thu Aug 15 10:14:03 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=4 ttl=55 time=16.899 ms
Thu Aug 15 10:14:04 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=5 ttl=55 time=16.242 ms
Thu Aug 15 10:14:05 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=6 ttl=55 time=16.574 ms
Run Code Online (Sandbox Code Playgroud)

-L 1选项使xargs一次处理一行而不是单词.


Cli*_*ntm 8

在 macos 上你可以做

ping --apple-time 127.0.0.1

输出看起来像

16:07:11.315419 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.064 ms
16:07:12.319933 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.157 ms
16:07:13.322766 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.066 ms
16:07:14.324649 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.148 ms
16:07:15.328743 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.092 ms
Run Code Online (Sandbox Code Playgroud)


小智 7

尝试这个:

ping www.google.com | while read endlooop; do echo "$(date): $endlooop"; done
Run Code Online (Sandbox Code Playgroud)

它返回如下内容:

Wednesday 18 January  09:29:20 AEDT 2017: PING www.google.com (216.58.199.36) 56(84) bytes of data.
Wednesday 18 January  09:29:20 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=1 ttl=57 time=2.86 ms
Wednesday 18 January  09:29:21 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=2 ttl=57 time=2.64 ms
Wednesday 18 January  09:29:22 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=3 ttl=57 time=2.76 ms
Wednesday 18 January  09:29:23 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=4 ttl=57 time=1.87 ms
Wednesday 18 January  09:29:24 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=5 ttl=57 time=2.45 ms
Run Code Online (Sandbox Code Playgroud)


小智 6

在OS X上,您只需使用--apple-time选项:

ping -i 2 --apple-time www.apple.com
Run Code Online (Sandbox Code Playgroud)

产生如下结果:

10:09:55.691216 64 bytes from 72.246.225.209: icmp_seq=0 ttl=60 time=34.388 ms
10:09:57.687282 64 bytes from 72.246.225.209: icmp_seq=1 ttl=60 time=25.319 ms
10:09:59.729998 64 bytes from 72.246.225.209: icmp_seq=2 ttl=60 time=64.097 ms
Run Code Online (Sandbox Code Playgroud)

  • 这不处理超时。 (2认同)