How to Delay Pipe Netcat to Connect on First Input

How to delay pipe netcat to connect on first input

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.

netcat closes connection after 8192 bytes

This is happening because netcat is receiving end-of-file from its standard input. That is, the command echo "something" causes the string something\n to be sent to the pipe connected to netcat's standard input; then the pipe is closed (because the echo command terminated). So, on the first read of the pipe, netcat will receive that string, but on its next read, it will receive EOF. This causes it to break its connection to the peer even though the peer may not be finished sending.

Essentially, after being started as above, netcat will keep sending its standard input to the socket, and the socket to its standard output until one of them is closed. Then it exits.

So you simply need to do something to ensure that netcat doesn't receive EOF on its standard input before it gets EOF on the socket. Something like this will likely do it:

(echo "something" ; sleep 1) | netcat localhost $myport

Now the output of echo "something" is sent to the pipe connected to netcat's input, but the pipe won't actually be closed until the sleep 1 also completes since both commands are started in a sub-shell which is connected to the write end of the pipe. (You might need to tinker with the number of seconds slept if the amount to be sent by the peer is large.)

Multiple parameters with delay in NetCat

The pipe ("|") redirects the output of one command to the input of another command, so echo get_video_state | echo get_resolution | nc -u -i 1 -w 5 192.168.xxx.xx 60000 wont work, because output of first echo is redirected to the second echo. You have to run the commands separately and then redirect their output to netcat. You can do this in this way:

(echo get_video_state & echo get_resolution) | nc -u -i 1 -w 5 192.168.xxx.xx 60000

Force netcat to send messages immediately (without buffering)

You can use the stdbuf command:

stdbuf -o0 python script.py | stdbuf -i0 nc -lk 9999

or the -u Unbuffered option for python:

python -u script.py | stdbuf -i0  nc -lk 9999

I/O buffering is, unless a program explicitly handles it on it's own, handled by the libc. stdbuf influences that behaviour. Further reading man stdbuf.

How to assign awk output to netcat with delay after every element?

You can use system within awk to execute shell commands such as sleep.

awk -F, '{system("sleep 1");print  $1}' sample.csv | netcat -lk 9999

A word of warning though, using system can sometimes make it difficult to cancel a command half way through with ^C as it cancels the system call but not awk.

Using netcat with FIFOs

try piping to tee instead of redirecting the output, man 1 tee, see what happens.

The following worked

$ tail -f /d/pc2dev | stdbuf -i0 -o0 netcat <IP> <Port> | stdbuf -i0 -o0 tr '\r' '\n' > /d/dev2pc



Related Topics



Leave a reply



Submit