2

I would like to delegate netcat stdin and stdout to a function.

I can do the following to redirect stdout only

nc -l -p 1234 | my_function

I can do the following to redirect stdin only

nc -l -p 1234 < <(my_function)
Francis
  • 3,335
  • 20
  • 46
  • should be easy to do the wiring with python/perl/ruby, but I guess it's also doable with bash... named pipes perhaps? – Karoly Horvath Jun 12 '14 at 18:00
  • possible duplicate of [how to redirect stdout of 2nd process back to stdin of 1st process?](http://stackoverflow.com/questions/5129276/how-to-redirect-stdout-of-2nd-process-back-to-stdin-of-1st-process) – Karoly Horvath Jun 12 '14 at 18:10

1 Answers1

4

When my_function is wrapped as an executable form (file), you can use -e option of netcat:

nc -l -p 1234 -e my_function

However if it's a function in bash, starting Bash 4.0 you can use coproc:

coproc my_function
nc -l -p 1234 <&"${COPROC[0]}" >&"${COPROC[1]}"
konsolebox
  • 72,135
  • 12
  • 99
  • 105