1

I'm trying to write a script where I cat the content of a file and pipe it into nc:

$cat somefile.txt | nc server port

The file content is sent to the server as expected but on the terminal I only see the response of the server.

What do I need to do such that I also see what I've sent.

I get a different response for every line from somefile.txt and i would like to have everything together.

2

You can use tee and take advantage of process substitution:

$ cat somefile.txt | tee >(nc server port)

tee simply copies its input to both stdout and a file.

With <() (bash process substitution) you're telling bash to write to the input of a command instead of a file.

0

You can use tee and avoid process substitution:

$ ( cat somefile.txt | tee /dev/fd/3 | nc host port ) 3>&1

The benefit of this is that nc will run synchronously. Here's an example of async process substitution behavior:

user@hostname $ cat somefile.txt
GET / HTTP/1.0
Host: StackOverflow.com

user@hostname $ cat somefile.txt | tee >(nc stackoverflow.com 80); echo "DONE"
GET / HTTP/1.0
Host: StackOverflow.com

DONE
user@hostname $ HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/
[...]

Notice that it says "DONE" and draws a prompt in the middle of the output. This is not an issue if nc runs synchronously:

user@hostname $ ( cat somefile.txt | tee /dev/fd/3 | nc stackoverflow.com 80 ) 3>&1; echo "DONE"
GET / HTTP/1.0
Host: StackOverflow.com

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/
[...]
DONE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.