Redirect process stdin and stdout to netcat

Daniele Pallastrelli picture Daniele Pallastrelli · Mar 30, 2016 · Viewed 16.2k times · Source

I have an embedded linux application with a simple interactive command line interface.

I'd like to access the command line from telnet (or network, in general).

However, the process should be started when the board turns on, and in a unique instance. So, the following netcat command is not an option:

nc -l -p 4000 -e myapp

I can do

nc -l -p 4000 | myapp

to send remote commands to myapp, but this way I can't see myapp output.

Is there any way to redirect both stdin and stdout to netcat?

Thanks.

Answer

Daniele Pallastrelli picture Daniele Pallastrelli · Mar 31, 2016

I found that by using bash v. >= 4.0 I can use coproc:

#!/bin/bash

coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"

EDIT

I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli