Question

Running in bash under Ubuntu:

I have a source that generates me some output, but not straight away. Let's assume it is a first netcat listening on a socket: netcat -l 12345.

And I would like to pipe it to an outgoing netcat (connecting over TCP), e.g. netcat -l 12345 | netcat localhost 54321. But the tricky bit is, that I know there is nothing listening for that incoming connection on localhost 54321 when I run the command, but I know there will be one when the first actual character arrives through the pipe.

So my question is: is there a way either:

  • to delay the execution of the outgoing netcat until the first character arrives into the pipe, or
  • to delay the outgoing netcat from trying to establish the TCP connection until it receives the first character on its standard input? (no straight option for that in man, switching to UDP is not acceptable)

Thanks in advance!

Edit: In reality, the source is more complex than a netcat, namely it is a listening netcat piped through all sort of stream modification.

Was it helpful?

Solution

Using the research you already did and that I commented to (by not knowing it was an answer to your own question), here is the full delayed_netcat.sh:

#!/bin/bash
read line
netcat "${@}" < <(echo $line ; cat)

This first waits for a line of input and later prepends that line using a simple echo to the "newly generated" input to the actual netcat. The rest of stdin is just redirected using cat which slurps it from stdin and adds it to the input of netcat. It also supports passing commandline options and arguments to the "real" netcat.

The usage is as follows:

netcat -l 12345 | cmd1 | cmd2 | ... | ./delayed_netcat.sh localhost 54321

The netcat is delayed till the first line is read. If you really want to start it after the first character is read the parts with read and echo need some rewrite.

OTHER TIPS

Port Forwarding or Port Mapping with netcat:

ncat -l -p 12345 -c 'ncat localhost 54321'

Using socat:

socat TCP4-LISTEN:12345 TCP4:localhost:54321

This command exits after the first connection is done.

I have found an answer to my question, but it is awful... so still looking for something better.

netcat -l 12345 | gawk '(NR==1){print""}{print;fflush()}' | ./delayed_netcat.sh

where ./delayed_netcat.sh:

#!/bin/sh
read line
netcat localhost 12345

So the read line delays the netcat localhost 12345 by waiting for and consuming the first input line, and I use gawk '(NR==1){print""}{print;fflush()}' to insert an empty line just before the first record... I'm sure there is room for much improvement to that!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top