由于某些原因我无法将基于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
如果你的 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
(因此也会被时间戳记)。 - Dennis Williamson来自 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
将其转换为所需的格式。
在 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 -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 &
我的原始提交不正确,因为它没有对每行日期进行评估。已做出更正。
请尝试这个。
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
命令。 - Thomas Szteligats(1)
命令(大多数发行版都已经默认安装了该软件)。$ 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
或者
ping 1.1.1.1 -I eth0 | ts "[%FT%X]"
允许使用与shell/date
解决方案相同的strftime格式字符串。
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 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
awk
: ping host | awk '{if($0 ~ /bytes from/){print strftime()"|"$0}else print}'
bash
作为你的 shell? - richk|
)之前将stderr
重定向到stdout
,例如:ping $host 2>&1 | while read pong; do echo "$(date): $pong"; done
。如果您希望将其写入(或追加)到文件中,可以在done
后面将整个命令重定向。此外,如果您不想生成子shell,则date
命令支持输出任意输入,例如:ping $host 2>&1 | while read pong; do date "+%c: $pong"; done
。请注意,date
的format
参数(以+
开头)可以随意自定义。有关更多信息,请参见man date
。 - 7heo.tkping google.fr | while read pong; echo (date)": $pong"; end
- Byscripts