1

I open a terminal window and connect to shell using netcat:

Terminal Window 1: nc hostname port

Then, I navigate to a directory and open a file,... etc (I would need to be able to do this myself, not automate it).

Can I then pipe the output of an executable on my computer to the netcat connection?

1
  • I don't think you can do that. A simple wrapper may help though: while read l; do case "$l" in "!"*) ${l#\!};;*) echo "$l"; esac; done | netcat host port. Any line of the form !program will pipe the output of 'program' to the connection instead of writing the line. Change the ! to something else if it conflicts with commands you're sending to your shell.
    – user313992
    Commented Sep 29, 2018 at 18:52

1 Answer 1

0

You could probably do it with a named pipe.

mkfifo ncpipe
nc hostname port < ncpipe
# --- In a galaxy far, far away (another terminal) ---
somecommand > ncpipe

However there are some issues with this... like, if you take too long, the connection might time out or whatever.

It's also possible to do this bi-directionally... I've used this some time ago to forward a local telnet port, not sure if it still works:

mkfifo send receive
> send &
> receive &
nc 127.0.0.1 23 < receive > send &
telnet_pid=$!
nc host port < send > receive &
server_pid=$!
wait $server_pid
kill $telnet_pid
rm send receive

Anyway, you can do weird things with named pipes. However you also have to be careful with them... if there is an old process that has the pipe open, unexpected output can appear. Best to create a shiny new clean pipe for each task and delete when done.

Port forwards in particular are done more easily (and securely) with SSH, but it's not always available. If available in your scenario, it might be preferable to just do:

somecommand | ssh user@host somereceiver
# or the other way around
ssh user@host somecommand | somereceiver

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .