title: A script to test for traffic from a streaming server?
body: <p>I have an instrument that streams data out a tcp port, and I'd like to use standard tools in a script to determine if the stream is available.  </p>
  
  <p>Manually I use <code>ncat</code>, which promptly exits if the data stream isn't available.</p>
  
  <p>Here's my initial bash script:</p>
  
  <pre><code>#!/bin/bash
  ncat somehost 1234 &gt;/dev/null &amp;
  pid=$!
  sleep 1
  if [ -d /proc/$pid/ ]; then
    kill -KILL $pid
    echo "It's alive, ALIVE\!"
    # Launch clients
  else
    echo "He's dead, Jim."
    # Perform resurrection
  fi
  </code></pre>
  
  <p>It works, but I'm wondering if there is a simpler or better way to accomplish this that doesn't rely on job control or procfs or even ncat.  I'd also like to know how much data was sent before a timeout exires.</p>
  
  <p>And, yes, in bash <code>ncat host port</code> can be replaced by <code>cat &lt;/dev/tcp/host/port</code>, but I'd also like to avoid bash-isms (so it can work under busybox).</p>
  
  <p>Another approach would be to use <code>wc</code> to count the lines/chars output by <code>ncat</code>, since it outputs only one line and exits if the connection can't be made.  But I can't wait forever for termination if the stream is up, so I'd need to use something like <code>timeout</code>, which has its own complications when trying to access the output of a command.</p>
  
  <p>Is there a "plain" approach that works with minimal dependencies?</p>
  
  <p>Or should I write a simple tool instead?  It would return the number of bytes read if a connection was made to host/port, a negative errno otherwise, and would support a wait time and protocol spec (tcp/udp).  A return value of 0 would mean the connection was made, but no data arrived before the wait time expired (a good thing to know).</p>
  
  <p>Or maybe patch <code>ncat</code> to do the above?</p>