Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm on a connection that sometimes gets interrupted for a while, and I'm trying to determine exactly when using ping, which generates output like this:

64 bytes from icmp_req=1196 ttl=64 time=4.64 ms
64 bytes from icmp_req=1197 ttl=64 time=5.14 ms
64 bytes from icmp_req=1198 ttl=64 time=4.90 ms
64 bytes from icmp_req=1199 ttl=64 time=25293 ms < -- interruption
64 bytes from icmp_req=1200 ttl=64 time=24286 ms      starts here
64 bytes from icmp_req=1201 ttl=64 time=23278 ms
64 bytes from icmp_req=1202 ttl=64 time=22270 ms
64 bytes from icmp_req=1203 ttl=64 time=21262 ms
64 bytes from icmp_req=1204 ttl=64 time=20254 ms
64 bytes from icmp_req=1224 ttl=64 time=142 ms
64 bytes from icmp_req=1225 ttl=64 time=4.87 ms
64 bytes from icmp_req=1226 ttl=64 time=4.54 ms

How can I?

  • Output only those lines with a large time. Can grep do it?
  • Add a timestamp to them (I know about the -D option of ping, but I'd prefer a human-readable stamp)
share|improve this question
what platform is this? Windows? – uSlackr May 29 '12 at 20:42
Linux, see tags – Bart van Heukelom May 29 '12 at 20:47
Windows answer removed – uSlackr May 29 '12 at 20:58
@uSlackr But thanks for trying to help :) – Bart van Heukelom May 29 '12 at 21:01
up vote 1 down vote accepted

Worked out this solution with grep and awk (ripped from this question, maybe overkill but it works)

ping \
| grep -E "time=[0-9]{2}" --line-buffered \
| gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
share|improve this answer

Speaking of overkill solutions, if you'd still like an answer to your first question (only get lines with a time greater than X), try the following ugly bash-python hybrid script:


IFS='\n' read -r -d '' check_ping <<EOF
from sys import stdin
data = reduce(lambda x, y: str(x) + str(y), stdin.readlines())
time = int(float(data.split("time=")[-1].split(" ")[0].strip()))
if time > $2: print(data)

ping -c 1 $1 | python -c "${check_ping}"

If you put that into a script file, say, you can pass it two parameters: the hostname you want to ping, and the minimum TTL, below which it shouldn't echo the ping output. For example, if you run 100 it will only return pings to that take more than 100ms.

share|improve this answer

I don't like the idea of processing text for this kind of thing although it is the unix-y thing to do. You might look at perl & Net::Ping. With it you can define your own timeout and only take action whenever the ping fails. More here:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .