bash 我如何为每个 ping 结果加上时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10679807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I timestamp every ping result?
提问by John Mee
Pingreturns this by default:
Ping默认返回这个:
64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
Is there some way I can get it to add the timestamp?
有什么方法可以让它添加时间戳吗?
For example,
例如,
Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
I'm on OS X v10.7 (Lion) which seems to have some BSDversion of ping.
我在 OS X v10.7 (Lion) 上,它似乎有一些BSD版本的 ping。
回答by richk
I could not redirect the Perl based solution to a file for some reason so I kept searching and found a bash
only way to do this:
由于某种原因,我无法将基于 Perl 的解决方案重定向到文件,因此我一直在搜索并找到了bash
唯一的方法:
ping www.google.fr | while read pong; do echo "$(date): $pong"; done
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
The credit goes to https://askubuntu.com/a/137246
回答by Paused until further notice.
If your AWK doesn't have strftime()
:
如果您的 AWK 没有strftime()
:
ping host | perl -nle 'print scalar(localtime), " ", $_'
To redirect it to a file, use standard shell redirection and turn off output buffering:
要将其重定向到文件,请使用标准 shell 重定向并关闭输出缓冲:
ping host | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > outputfile
If you want ISO8601 format for the timestamp:
如果您想要时间戳的 ISO8601 格式:
ping host | perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile
回答by ДМИТРИЙ МАЛИКОВ
From man ping
:
来自man ping
:
-D Print timestamp (unix time + microseconds as in gettimeofday) before each line.
It will produce something like this:
它会产生这样的东西:
[1337577886.346622] 64 bytes from 4.2.2.2: icmp_req=1 ttl=243 time=47.1 ms
Then timestamp could be parsed out from the ping
response and converted to the required format with date
.
然后时间戳可以从ping
响应中解析出来并转换为所需的格式date
。
回答by xuanyuanaosheng
terminal output:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}'
file output:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt
terminal + file output:
ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' | tee test.txt
file output background:
nohup 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: {}'
文件输出:
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 &
回答by Bromo
My original submission was incorrect because it did not evaluate date for each line. Corrections have been made.
我的原始提交不正确,因为它没有评估每一行的日期。已进行更正。
Try this
尝试这个
ping google.com | xargs -L 1 -I '{}' date '+%+: {}'
produces the following output
产生以下输出
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
The -L 1 option causes xargs to process one line at a time instead of words.
-L 1 选项使 xargs 一次处理一行而不是单词。
回答by Nicolas Grison
On OS X you can simply use the --apple-time option:
在 OS X 上,您可以简单地使用 --apple-time 选项:
ping -i 2 --apple-time www.apple.com
Produces results like:
产生如下结果:
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
回答by nutria
Try this:
尝试这个:
ping www.google.com | while read endlooop; do echo "$(date): $endlooop"; done
It returns something like:
它返回如下内容:
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
回答by Clintm
On macos you can do
在 macos 上你可以做
ping --apple-time 127.0.0.1
ping --apple-time 127.0.0.1
The output looks like
输出看起来像
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
回答by Prince John Wesley
Pipe the result to awk
:
将结果通过管道传输到awk
:
ping host | awk '{if(while true
do
echo -e "`date`|`ping -n -c 1 <IP_TO_PING>|grep 'bytes from'`"
sleep 2
done
~ /bytes from/){print strftime()"|"##代码##}else print}'
回答by Venkat Madhav
You did not specify any time stamp or interval for how long you would require such output, so I considered it to be an infinite loop. You can change it accordingly as per your need.
您没有指定任何时间戳或间隔需要多长时间才能获得此类输出,因此我认为这是一个无限循环。您可以根据需要相应地更改它。
##代码##