~/.bashrc
文件中创建一个函数,这样您就可以在控制台上使用ping-t
命令:function ping-t { ping "$1" | while read pong; do echo "$(date): $pong"; done; }
ping-t example.com
2018年3月31日12:58:31 CEST:PING example.com (93.184.216.34),数据大小为56(84)字节。
2018年3月31日12:58:31 CEST:来自93.184.216.34 (93.184.216.34)的64字节:icmp_seq=1 ttl=48 time=208毫秒
2018年3月31日12:58:32 CEST:来自93.184.216.34 (93.184.216.34)的64字节:icmp_seq=2 ttl=48 time=233毫秒
您没有指定需要输出的时间戳或间隔,因此我认为它是一个无限循环。根据您的需求进行相应更改即可。
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
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
来取消它。
tee
是个好主意,但是-c 1
的问题在于会丢失整体统计数据... - minusf只需使用sed和循环:
ping google.com|while read; do sed -r "s/(.*)/$(date) \1/g"<<<"$REPLY";done
grep
部分更改为egrep '(bytes from|errors)'
。 - rubo77