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(Lion)上,它似乎具有ping的某些BSD版本。
Answers:
由于某种原因,我无法将基于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
bash
外壳?
stderr
到stdout
管道(|
)之前的位置,如下所示:ping $host 2>&1 | while read pong; do echo "$(date): $pong"; done
。如果您希望将其写入(或追加)到文件中,则可以重定向整个命令(完成后)。另外,如果您不希望生成子shell,则该date
命令支持echo
ing任意输入,例如:ping $host 2>&1 | while read pong; do date "+%c: $pong"; done
。请注意format
的参数date
(即打头+
)可以随意定制。请参阅man date
以获取更多信息。
如果您的AWK没有strftime()
:
ping host | perl -nle 'print scalar(localtime), " ", $_'
要将其重定向到文件,请使用标准的shell重定向并关闭输出缓冲:
ping host | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > outputfile
如果您想要ISO8601格式的时间戳记:
ping host | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile
ping
仅接收到SIGPIPE
。您可以使用进程替换代替管道:ping host > >(perl -nle 'print scalar(localtime), " ", $_')
Ctrl-C可以ping
执行您想要的操作。您可以对while
循环执行相同的操作。顺便说一句,在我的系统上,摘要转到STDOUT
而不是STDERR
(因此也加上了时间戳)。
use Time::Piece; print localtime->datetime
(和其他适当的设置)从5.10开始或使用CPAN模块或strftime
。
来自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
。
终端输出:
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
终端+文件输出:
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 &
在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
我的原始提交内容不正确,因为它没有评估每一行的日期。已更正。
试试这个
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一次处理一行而不是单词。
ping -D -n -O -i1 -W1 8.8.8.8
试试这个:
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
在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
我还需要它来监视网络问题,以解决数据库镜像超时问题。我使用以下命令代码:
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/
尝试这一行。
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取消它。
tee
,但问题-c 1
是失去了总体统计数据……
您可以在~/.bashrc
文件中创建一个函数,以便ping-t
在控制台上收到ping命令:
function ping-t { ping "$1" | while read pong; do echo "$(date): $pong"; done; }
现在,您可以在控制台上调用此命令:
ping-t example.com
Sa 31.2018年CEST 12:58:31 CEST:PING example.com(93.184.216.34)56(84)个字节的数据。
Sa31.Mär12:58:31 CEST 2018:来自93.184.216.34(93.184.216.34)的64个字节:icmp_seq = 1 ttl = 48 time = 208 ms
Sa31.Mär12:58:32 CEST 2018:来自93.184的64个字节.216.34(93.184.216.34):icmp_seq = 2 ttl = 48时间= 233毫秒