3

It would appear that the output from hping is fully buffered when piped to perl for further line-by-line processing, so piping hping to perl doesn't work.

hping --icmp-ts example.ca | perl -ne 'if (/Originate=(\d+) Receive=(\d+) Transmit=(\d+)/) { ($o, $r, $t) = ($1, $2, $3); } if (/tsrtt=(\d+)/) { print $r - $o, " ", $o + $1 - $t, "\n"; }'

How do I change hping from being fully buffered to being line buffered when piped?

Not a duplicate of the following question, since no solution works in OpenBSD base:

Turn off buffering in pipe

cnst
  • 3,283

2 Answers2

6

There are two common solutions, stdbuf and unbuffer.

stdbuf is from GNU coreutils, it was added in version 7.5 in 2009 so it has made its way to all current non-embedded Linux systems apart from CentOS 5. It is also in FreeBSD since version 8.4. No other unix variant has adopted it yet that I know of, and in particular not OpenBSD as of 5.4.

unbuffer is an Expect script, and as such is available everywhere Expect is available, which includes pretty much any unix. All BSD variants have it in their port collection, in the expert package.

So install the expect package and run unbuffer hping … | perl …

  • 1
    Interestingly, OS X 10.8 on mid-2013 MacBook Air does include expect version 5.45 in /usr/bin/expect, however, unbuffer seems to be missing. – cnst Nov 25 '13 at 02:22
3

Another option may be to use socat:

# cf. http://unix.stackexchange.com/a/25377
socat EXEC:'hping --icmp-ts example.ca',pty,ctty STDIO | perl -ne 'if ...
vron
  • 31