Skip to content

Instantly share code, notes, and snippets.

@tmcolby
Last active May 23, 2019 20:49
Show Gist options
  • Save tmcolby/4bfb10b3496fb314f58971a0e0a0de50 to your computer and use it in GitHub Desktop.
Save tmcolby/4bfb10b3496fb314f58971a0e0a0de50 to your computer and use it in GitHub Desktop.
socat tricks - relaying serial ports, sniffing/monitoring/data logging traffic
This first example assumes two usb serial port adapters are connected to the host machine. They enumerate as ttyUSB0 and ttyUSB1.
We want data coming into ttyUSB0 to be piped right back out of ttyUSB1 (so our host sits as a man in the middle).
We want an application running on the host to be able to connect to a com port and monitor the traffic.
# create the fifo
sudo mkfifo -m 777 /tmp/fifo
# stream serial data on /dev/ttyUSB0 to the fifo AND a virtual com port /tmp/relay
sudo socat /dev/ttyUSB0,raw,echo=0 system:'sudo tee /tmp/fifo | sudo socat - "pty,raw,echo=0,link=/tmp/relay"' &
# read from the fifo and stream it back out /dev/ttyUSB1
sudo cat /tmp/fifo | sudo socat - "/dev/ttyUSB1,raw,echo=0" &
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This example takes data in on ttyUSB0 and records the input and output to log files as well as relaying it to a virtual com
port so that an application running on the host machine can connect to the virtual com port.
touch in.log
touch out.log
sudo socat /dev/ttyUSB0,raw,echo=0 system:'tee in.log | socat - "pty,raw,echo=0,link=/tmp/relay" | out.log' &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment