TL;DR: Yes, but you'll need something smarter than a piped cat
cat
simply concatenates all files given to it and dumps them to stdout
, -
represents stdin so the way your referenced Q/A pair worked was by concatenating stdin to the file.
netcat
is a fine tool for what you're trying to do as far as opening up a raw pipe to the other port. I think a better question for you would be, is piped output from cat
the right tool? No.
The problem with just using cat ... | netcat ...
is that its a one way deal. The output of cat
is used as the input to netcat
but the output will go to stdout
, the pipe is not two way, you need interactivity.
If you want to interactively perform actions based on responses from the server over your netcat pipe there are a whole host of ways to programmatically interact with a pipe.
You could use read
for instance, or expect
.
If you're willing to expand your tool chest a little bit i suggest using ncat
which is a more modern implementation of netcat
and has a handy option -e
which allows the attaching of a command or script to the netcat
pipe. If you cannot get your hands on a netcat
with -e
you may need to learn a few things about named pipes and I/O Redirection to get the same effect.
I wanted to test some of this stuff before writing this answer, see below for testing/examples
I didn't write a listening server that handled multiple files sent via netcat
, I'm just going to assume you have that working, but I can simulate some programmatic interaction from the client side and do the server by hand.
My dummy "client"/"server" interaction here is based on this fun activity of seting up 2 ncat sessions to talk to each other
I wrote this simple interactive script:
#!/bin/bash
i=0
while true; do
read line
if [[ $line == "goose" ]]; then
echo "zoom"
exit 0
else
i=$(expr $i + 1)
echo "$i ..."
fi
done
I start my "server" ncat -l -p 1337
- I start my "client" controlled by the interactive script
ncat localhost 1337 -e ./interact.sh
And I operate the "server" by hand (since ncat doesn't clearly show I vs O I've notated Input with i:
and output with o:
):
i:duck
o:1 ...
i:duck
o:2 ...
i:duck
o:3 ...
i:goose
o:zoom