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.
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
:So the
read line
delays thenetcat localhost 12345
by waiting for and consuming the first input line, and I usegawk '(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!Port Forwarding or Port Mapping with netcat:
Using
socat
:This command exits after the first connection is done.
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
:This first waits for a line of input and later prepends that line using a simple
echo
to the "newly generated" input to the actualnetcat
. The rest of stdin is just redirected usingcat
which slurps it fromstdin
and adds it to the input ofnetcat
. It also supports passing commandline options and arguments to the "real"netcat
.The usage is as follows:
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 withread
andecho
need some rewrite.