0

is there any solution for

nc 192.168.1.1 1234 to retry till it gets connected and once it gets connected ; it will send an output via "nc 192.168.1.1 1234" on machine 192.168.1.1 where it is listening on port no. 1234 .

??

//I want to send a message over "nc" once the connection is created by nc command else it will try reconnecting for connection via nc//

2 Answers 2

3

If you are using bash or similar shell, you could do something like the following;

cat message | while ! nc 192.168.1.1 1234 ; do sleep 1 ; done
0
0

I had a similar problem, but for curl. I built a dedicated tool called retry to do this. Retry reads stdin once and once only, repeats your nc command until success, then on success outputs stdout once and once only to the next command in the pipeline.

~$ echo output | retry -- nc 192.168.1.1 1234
retry: nc returned 1, backing off for 10 seconds and trying again...
retry: nc returned 1, backing off for 10 seconds and trying again...
^C

https://github.com/minfrin/retry

Available out the box in recent Debian, Ubuntu, and Nix.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.