Unexpected redirect result with 2>&1 and command p

2019-09-05 23:23发布

问题:

I was playing around with nc and since the -e command is gone, it recommended

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

I thought that the "2>&1" is unnecessary because I don't need to know the errors of the simple commands I was going to run like "whoami", "ls", etc. so my command instead is

cat /tmp/f | /bin/sh -i | nc -l 127.0.0.1 1234 > /tmp/f

But then, I don't get "clean" results. This is what I get

SERVER:

Listening on [0.0.0.0] (family 0, port 1234)
$  $

Client:

nc localhost 1234
ls 
testfile
whoami
user1

Basically, I don't see the $ in my client terminal. When I do the command with the 2>&1, I get what I expected

SERVER:

Listening on [0.0.0.0] (family 0, port 1234)

Client:

nc localhost 1234
$ ls 
testfile
$ whoami
user1

I know that 2>&1 means redirect stderr to stdout so in this case, how is the command prompt "$" part of the stderr? Shouldn't it be part of stdin?

回答1:

The shell uses the standard error stream for prompts, and also for menus created by select, and any other text that is meant for interacting with the user.



标签: bash shell