2

Because netcat on my box doesn't support -e, The netcat man page gives me this workaround:

$ rm -f /tmp/f; mkfifo /tmp/f
$ cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f

I don't understand how this works. I though that when you try to cat /tmp/f that that would block until something is written to /tmp/f and if that's blocked how does the rest of the command run?

1 Answers1

3

All commands in a pipeline are started simultaneously and run simultaneously – command 2 does not wait for command 1 to exit. Instead, pipelines rely on read operations blocking until the preceding command has produced some output.

For example, the output (stdout) of cat /tmp/f is directly1 connected to the input of /bin/sh -i; whenever sh tries to read from its stdin, it will block until cat has produced some output.


1 (Almost directly; cat's stdout and sh's stdin are tied to two ends of a 'pipe' object.)

grawity
  • 501,077