Send UDP messages with /dev/udp
Dev Tip 6/99
This post is copied here for reference, but now lives at tightlycoupled.io.
bash shell comes with two pseudo-devices for TCP and UDP network communication and
/dev/udp. To use either in
bash, you need to read or write to the device appending the host and port to the end of the path — e.g.,
/dev/tcp/google.com/80. The primary reason I use the pseudo-devices is it’s easier for me to remember than the netcat. But if I wanted a portable solution, then netcat is the winner. Either case, learning of
bash's TCP and UDP pseudo-devices tickled my brain.
For my day to day work, the
/dev/udp comes in handy for sending statsd metrics to the local
statsd server process on PlanGrid servers.
statsd has a wire protocol that looks like:
METRIC_NAME:METRIC_VAL|TYPE# Example: counting 200 status codes for nginx:
Here is how to send the above metric via
bash's UDP pseudo-device:
echo "nginx.status_200:1|c" >/dev/udp/127.0.0.1/8125
The other portable way is with netcat (
echo "nginx.status_200:1|c" | nc -u -w0 127.0.0.1 8125