# Piping binary data to an external program

**URL:** <https://www.ruby-forum.com/t/piping-binary-data-to-an-external-program/77229>\
**Category:** Ruby\
**Created:** [March 6, 2006, 10:18am UTC](https://www.ruby-forum.com/t/piping-binary-data-to-an-external-program/77229 "2006-03-06T10:18:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [March 6, 2006, 10:18am UTC](https://www.ruby-forum.com/t/piping-binary-data-to-an-external-program/77229/1 "2006-03-06T10:18:48Z")

</div>

Hello,

I’m working on a logparser and i’ve run into some issues. It will  
parse OpenBSD PF logs. They are tcpdump format logs and BSD normally  
compress them.

Here is the usage I have in mind:

“gzip -cd log.gz | ruby logparser.rb --today”

I have the following code:

Open3.popen3("/usr/sbin/tcpdump -nettr -") { |in\_io, out\_io, err\_io|  
in\_io.write($stdin.read)  
in\_io.close  
$log = out\_io.read  
}

The script freezes on the open3 line and doesn’t continue. I’ve tested  
several other methods but it doesn’t seem to work.

Any suggestions on how this can be done?

Regards,

Ricardo.

---

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [March 6, 2006, 10:49am UTC](https://www.ruby-forum.com/t/piping-binary-data-to-an-external-program/77229/2 "2006-03-06T10:49:10Z")

</div>

|MKSM| wrote:

> I have the following code:  
> Any suggestions on how this can be done?  
> It’s likely that you run into a deadlock caused by pipe buffer sizes. I  
> suggest to not read and write the whole content but to do it in chunks.  
> Also, I’d separate the reading and writing code into two threads.

Untested:

Open3.popen3("/usr/sbin/tcpdump -nettr -") { |in\_io, out\_io, err\_io|  
t = Thread.new(in\_io) do |out|  
while ( buff = $stdin.read( 1024 ) )  
out.write(buff)  
end  
out.close  
end  
$log = “”  
while ( b = out\_io.read(1024))  
$log \<\< b  
end  
}

HTH

Kind regards

```
robert
```
