I'm writing a script to automatically do the ping test. because I have hundreds of endpoint to ping each other, and I don't want the script stop running because the ping stuck, so I use timeout, and run the ping in background. and I also want to save the ping statistics to file. firstly I tried below commands:

timeout 20s ssh hostA "ping -c 30 -I " | awk '{print "from 50 to 28 |",$0;}' &

and I got the result shows in screen which is what I expected:

from 50 to 28 | PING ( from : 56(84) bytes of data.
from 50 to 28 | 64 bytes from icmp_seq=1 ttl=64 time=0.264 ms
from 50 to 28 | 64 bytes from icmp_seq=2 ttl=64 time=0.245 ms
from 50 to 28 | 64 bytes from icmp_seq=3 ttl=64 time=0.220 ms
from 50 to 28 | 64 bytes from icmp_seq=4 ttl=64 time=0.214 ms
from 50 to 28 | 64 bytes from icmp_seq=5 ttl=64 time=0.123 ms
Killed by signal 15.

but when I tried to save this output in file, it failed, below is the command I used:

timeout 20s ssh cic-1 "ping -c 30 -I " | awk '{print "from 50 to 28",$0;}' | tee -i -a path/timeout_test.txt &

but I got nothing in timeout_test.txt

I also tried command below, I created the timeout_test.txt before the command run.

timeout 20s ssh cic-1 "ping -c 30 -I " | awk '{print "from 50 to 28",$0;}' >> path/timeout_test.txt &

but still I got nothing.

  • hmmmmm.... I found that if I change the count of ping time to 3 (or any number that to finish the ping within 20s) the statistics can be written to file, but if the ping is ended by timeout, the result will not written to file. So new question is, how could I write the output to file even the ping ended by timeout? – Echo Jun 6 '17 at 7:20
  • 1
    You can edit your posts, please do so. – phk Jun 6 '17 at 8:14
  • Hundreds of endpoints ping-ing each other should be managed by dedicated software such as monit, not by ad-hoc scripts. – Satō Katsura Jun 7 '17 at 8:45

Maybe I would first think about that you're running the ping Command on the SSH Server/Remote PC, so that the result there should be used by tee -a command on a file on that remote. By the way instead of dropping/piping everything in one shell line I would prefer to use intermediate files and then to work on the log results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.