如何时间戳每平的结果?(How do I timestamp every ping result?)

2019-07-29 11:15发布

平安默认返回此:

64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

有一些方法可以让我得到它添加时间戳?

例如,

Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

我在OS X v10.7(狮子),这似乎有一些BSD平的版本。

Answer 1:

如果您AWK没有strftime()

ping host | perl -nle 'print scalar(localtime), " ", $_'

若要将其重定向到一个文件,使用标准的外壳重定向并关闭输出缓冲:

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

如果你想为时间戳ISO8601格式:

ping host | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile


Answer 2:

我不能重定向基于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

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



Answer 3:

man ping

   -D     Print timestamp (unix time + microseconds as in gettimeofday) before each line.

它会产生这样的:

[1337577886.346622] 64 bytes from 4.2.2.2: icmp_req=1 ttl=243 time=47.1 ms

然后时间戳可以从解析出ping响应,并转换为与所要求的格式date



Answer 4:

  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 &



Answer 5:

我最初提交的材料是不正确的,因为它没有评估日期每一行。 更正已作出。

试试这个

 ping google.com | xargs -L 1 -I '{}' date '+%+: {}'

产生以下输出

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

所述-L 1选项导致xargs的在一个时间,而不是字来处理一行。



Answer 6:

在OS X上,你可以简单地使用--apple时间选项:

ping -i 2 --apple-time www.apple.com

产生的结果,如:

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


Answer 7:

试试这个:

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

它返回是这样的:

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


Answer 8:

管结果awk

 ping host | awk '{if($0 ~ /bytes from/){print strftime()"|"$0}else print}'


Answer 9:

在Mac OS,你可以做

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


Answer 10:

您没有指定任何时间戳或间隔你多久需要这样的输出,所以我认为这是一个无限循环。 您可以相应地改变它根据自己的需要。

while true
do
   echo -e "`date`|`ping -n -c 1 <IP_TO_PING>|grep 'bytes from'`"
   sleep 2
done


Answer 11:

ping -D -n -O -i1 -W1 8.8.8.8

或者可能

while true; do \
    ping -n -w1 -W1 -c1 8.8.8.8 \
    | grep -E "rtt|100%" \
    | sed -e "s/^/`date` /g"; \
    sleep 1; \
done


Answer 12:

我也需要这个监控网络问题我的数据库镜像的超时问题。 我用命令代码如下:

ping -t Google.com|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 Google.com>nul" >C:\pingtest.txt

你只需要修改Google.com您的服务器名称。 它完美的作品对我来说。 记得要制止这种当你完成。 该pingtest.txt文件将通过每秒(约)1 KB增加。

感谢raymond.cc。 https://www.raymond.cc/blog/timestamp-ping-with-hrping/



Answer 13:

试试这条线。

while sleep 1;do echo "$(date +%d-%m-%y-%T) $(ping -c 1 whatever.com | gawk 'FNR==2{print "Response from:",$4,$8}')" | tee -a /yourfolder/pingtest.log;done

你必须用取消它ctrl-c寿。



Answer 14:

你可以在你创建一个函数~/.bashrc文件,所以你得到一个ping命令ping-t在控制台上:

function ping-t { ping "$1" | while read pong; do echo "$(date): $pong"; done; }

现在你可以在控制台上称之为:

ping-t example.com

萨31. MAR 12时58分31秒CEST 2018:PING example.com(93.184.216.34)数据的56(84)个字节。
萨31. MAR 12时58分31秒CEST 2018:从93.184.216.34 64个字节(93.184.216.34):icmp_seq = 1个TTL = 48时间= 208毫秒
萨31. MAR 12时58分32秒CEST 2018:从93.184.216.34 64个字节(93.184.216.34):icmp_seq = 2 TTL = 48时间= 233毫秒



文章来源: How do I timestamp every ping result?