13

I'd like to take a program P that reads from stdin & writes to stdout, but connect it to nc or whatever such that it reads from a certain port and outputs to another port.

# The reading is easy, here P reads from port 50505
nc -l 50505 | P

How do I get it to write back to say port 60606?

3

2 Answers 2

14

I you mean that someone may open 2 TCP connections to your machine, one to port 50505 and another to port 60606, send data on the first one intended to be fed to P and expect to read the output of P from the second TCP connection, then that would be:

< /dev/null nc -q -1 -l 50505 | P | nc -l 60606 > /dev/null

Or with socat:

socat -u tcp-listen:50505,reuseaddr - | P | socat -u - tcp-listen:60606,reuseaddr

For P to send its output back to the same connection:

socat tcp-listen:50505,reuseaddr exec:P
1
  • If you want socat to connect to a remote host instead of listening for an incoming connection, replace tcp-listen:PORT with tcp:HOST:PORT Dec 25, 2020 at 5:45
4

You don't need nc in order to work with ports. bash can do that itself:

Bash handles several filenames specially when they are used in redirections, as described in the following table:

/dev/fd/fd
    If fd is a valid integer, file descriptor fd is duplicated.
/dev/stdin
    File descriptor 0 is duplicated.
/dev/stdout
    File descriptor 1 is duplicated.
/dev/stderr
    File descriptor 2 is duplicated.
/dev/tcp/host/port
   If host is a valid hostname or Internet address, and port is an integer 
   port number or service name, bash attempts to open a TCP connection to
   the corresponding socket.
/dev/udp/host/port
    If host is a valid hostname or Internet address, and port is an integer 
    port number or service name, bash attempts to open a UDP connection to
    the corresponding socket.
3
  • 3
    /dev/tcp is often disabled and can't be used for listening. You need zsh if you want a shell that can create listening tcp sockets. Nov 10, 2014 at 22:30
  • @StéphaneChazelas I have to admit I never tried. It doesn't say anything about this restriction in the man page, does it? Nov 10, 2014 at 22:51
  • Well, how would you do it? A listener socket is a door to create more accepting sockets, redirections don't work as well for that. zsh uses a builtin for that (ztcp -l for listening and ztcp -a to accept connections on the listening socket). Nov 10, 2014 at 23:02

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.