From: Aaron Taylor Date: Sat, 27 Feb 2021 10:45:38 +0000 (-0800) Subject: Added note to UNIX Tips page explaining how to connect a process' STDIO to a TCP... X-Git-Url: http://git.subgeniuskitty.com/website_subgeniuskitty.com/.git/commitdiff_plain/08f3fbf9c77959fd830bba4f424d54fcd374035c Added note to UNIX Tips page explaining how to connect a process' STDIO to a TCP port. --- diff --git a/data/notes/unix_misc.md b/data/notes/unix_misc.md index 5b8b3a8..ea2a1bd 100644 --- a/data/notes/unix_misc.md +++ b/data/notes/unix_misc.md @@ -49,3 +49,42 @@ to a `(gdb)` prompt. Exit `gdb`. The characters have now been received by `vi` and a file should be waiting at `/tmp/vi_recover.txt`. + + +# Expose Process STDIO on TCP Port # + +This note explains how to launch a process and connect its +`stdin`/`stdout`/`stderr` directly to a TCP port. It was tested on FreeBSD 12. + +As an example, we will launch a `bash` process running on a Debian 10 machine +and connect it to TCP port `4242`. Then, from a FreeBSD machine we will connect +and utilize the `bash` session remotely. + +First, on the Debian machine, we use +[`socat`](http://www.dest-unreach.org/socat/), a program which establishes two +bidirectional byte streams and transfers data between them. In our case, we +tell `socat` to build a PTY at `/tmp/pty_test` and connect it to TCP port +`4242`. + + socat PTY,link=/tmp/pty_test,raw TCP-LISTEN:4242,reuseaddr,fork + +Note that the `PTY` argument to `socat` must precede the `TCP-LISTEN` argument. +Despite the bidirectional nature, the endpoint definition order is not +interchangeable. + +Now we use `setsid` to start a new `bash` process and attach it to the PTY at +`/tmp/pty_test`. Note that we collapse `stdout` and `stderr` into a single +combined stream. + + setsid sh -c 'exec bash --login <> /tmp/pty_test >&0 2>&1' + +Replace `bash --login` with whatever other command you desire to execute. + +Now, from the FreeBSD machine, connect the remote TCP port to your local STDIO +with `socat`. Assuming the Debian machine running `bash` is at `192.168.1.107`, +execute the following command. + + socat STDIO,raw TCP:192.168.1.107:4242 + +At this point you have a fully interative `bash` session running on the Debian +machine, controlled from the FreeBSD machine.