我如何为每个ping结果添加时间戳?
Ping默认返回:
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版本的平。
如果你的AWK没有strftime()
:
ping host | perl -nle 'print scalar(localtime), " ", $_'
要将其redirect到文件,请使用标准shellredirect并closures输出缓冲:
ping host | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > outputfile
如果你想要ISO8601格式的时间戳:
ping host | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile
我无法将基于Perl的解决schemeredirect到一个文件出于某种原因,所以我继续search,发现一个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
从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
响应中parsing时间戳,并将其转换为具有date
的所需格式。
-
terminal输出:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}'
-
文件输出:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt
-
terminal+文件输出:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' | tee test.txt
-
文件输出背景:
nohup ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt &
我原来的提交是不正确的,因为它没有评估每一行的date。 更正已经完成。
尝试这个
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一次处理一行而不是单词。
在OS X上,您可以简单地使用–apple-time选项:
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
将结果传递给awk
:
ping host | awk '{if($0 ~ /bytes from/){print strftime()"|"$0}else print}'
尝试这个:
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
你没有指定任何时间戳或间隔多长时间,你会需要这样的输出,所以我认为这是一个无限循环。 你可以根据你的需要相应地改变它。
while true do echo -e "`date`|`ping -n -c 1 <IP_TO_PING>|grep 'bytes from'`" sleep 2 done
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
我也需要这个监视networking问题,我的数据库镜像超时问题。 我使用如下的命令代码:
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/
在马科斯你可以做
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
尝试这一行。
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
tho取消它。