2

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

  • So unbuffer and stdbuf aren't available in BSD? – slm Nov 24 '13 at 12:23
  • 1
    @slm stdbuf isn't in OpenBSD. unbuffer is, but it isn't immediately obvious: you need to install the expect port. – Gilles Nov 24 '13 at 21:40
  • what, perl’s $|++ doesn’t work? – user2497 Oct 3 '17 at 4:56
  • @user2497, can you clarify how exactly that'd be used? – cnst Oct 3 '17 at 4:58
  • @cnst In the perl block, before the if condition. perldoc perlvar. – user2497 Oct 3 '17 at 4:59
4

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 …

  • 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 2:22
1

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 ...

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.