Send UDP messages with /dev/udp

Dev Tip 6/99

paydro
paydro
Aug 30, 2017 · 1 min read

This post is copied here for reference, but now lives at tightlycoupled.io.

The bash shell comes with two pseudo-devices for TCP and UDP network communication and/dev/tcp 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:
nginx.status_200:1|c

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 (nc):

echo "nginx.status_200:1|c" | nc -u -w0 127.0.0.1 8125

100 Days Of Development Tips

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store