Search code examples
shellsocat

How to redirect input and output between two instances of cat with socat


I'm learning socat and as an excercise try to send text between two different shell windows. The following works well:

socat - UNIX-LISTEN:uah
socat - UNIX-CONNECT:uah

I assumed that I could replace one - with EXEC:cat but that doesn't work:

socat EXEC:cat UNIX-LISTEN:uah
socat - UNIX-CONNECT:uah

The following stackexchange answer suggests that the EXEC should work though: https://unix.stackexchange.com/a/222934


Solution

  • Socats "addresses" work bidirectionally. The EXEC address connects stdin and stdout of the invoked process to Socat. Thus, your solution would just echo data entered on the second terminal (via Socat(2), UNIX domain socket, Socat(1), cat, back to Socat(1), UNIX socket, and Socat(2)) back to the second terminal.

    To play with EXEC:cat, you'd better start with unidirectional mode, e.g.:

    socat -u UNIX-LISTEN:uah EXEC:cat
    socat -u - UNIX-CONNECT:uah
    

    A bidirectional example is this:

    set +H  # turn off special handling of ! in bash
    socat UNIX-LISTEN:uah EXEC:cat!!EXEC:cat
    socat - UNIX-CONNECT:uah
    

    Here, the first EXEC:cat gets the terminals input and passes it to Socat, the second one passes data from Socat to stdout.