February 15, 2013

TIL about Bash sockets

A couple of days ago, I needed to write a one line bash script which would send data over to a Graphite or Statsd. This could easily be solved with netcat:

echo "some.valuable.metric 1 date +%s" | nc graphite.server.com 2003

What about portability? Netcat isn't installed on every machine. The search for an alternative method to send metrics lead me to Bash sockets. With a Bash socket, you can send the in this manner:

echo "some.valuable.metric 1 date +%s" > /dev/tcp/graphite.server.com/2003

Discovering this gem has inspired me to review the bash manual.