Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 192.168.0.1: icmp_req=1196 ttl=64 time=4.64 ms
64 bytes from 192.168.0.1: icmp_req=1197 ttl=64 time=5.14 ms
64 bytes from 192.168.0.1: icmp_req=1198 ttl=64 time=4.90 ms
64 bytes from 192.168.0.1: icmp_req=1199 ttl=64 time=25293 ms < -- interruption
64 bytes from 192.168.0.1: icmp_req=1200 ttl=64 time=24286 ms      starts here
64 bytes from 192.168.0.1: icmp_req=1201 ttl=64 time=23278 ms
64 bytes from 192.168.0.1: icmp_req=1202 ttl=64 time=22270 ms
64 bytes from 192.168.0.1: icmp_req=1203 ttl=64 time=21262 ms
64 bytes from 192.168.0.1: icmp_req=1204 ttl=64 time=20254 ms
64 bytes from 192.168.0.1: icmp_req=1224 ttl=64 time=142 ms
64 bytes from 192.168.0.1: icmp_req=1225 ttl=64 time=4.87 ms
64 bytes from 192.168.0.1: 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
add comment

3 Answers

up vote 1 down vote accepted

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

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

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:

#!/bin/bash

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)
EOF

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

If you put that into a script file, say test.sh, 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 tst.sh www.google.com 100 it will only return pings to www.google.com that take more than 100ms.

share|improve this answer
add comment

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:

http://perldoc.perl.org/Net/Ping.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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