Usage of nc with timeouts in ms
I don't think it is possible with nc
alone. But you can additionally use the timeout
tool (GNU coreutils package) which allows you to run a command with a timelimit specified as a floating point number, e.g. like so.
echo -n read_input | timeout 0.5 nc 192.168.1.185 8800
Related videos on Youtube
Mark
Updated on September 18, 2022Comments
-
Mark over 1 year
I'm using
nc
in a Debian environment:# dpkg -l | grep netcat ii netcat-traditional 1.10-41+b1 # cat /etc/debian_version 9.4
Its help page describe the behavior of the
-w
flag:-w secs timeout for connects and final net reads
So it seems it's accept seconds only (as integer). I need to reduce this timeout because I have to poll a remote device twice per second:
echo -n read_input | nc -w 1 192.168.1.185 8800
this command is sent by an application. I can only set the console command to be executed.
Of course, with a timeout of 1 second I can barely poll the device about 1 time every two seconds (to avoid to open a new socket when the previous is not closed yet).
Do you confirm there's no way to achieve this? So, is there a way to have a timeout < 1 second with
netcat
? For my own curiosity: why a network timeout should be in seconds?-
Rui F Ribeiro over 5 yearsWhat is exactly the question? What is the intended use?
-
Mark over 5 years"Is there a way to have a timeout less than 1 second with
nc
?"
-
-
Mark over 5 yearsAs far as I know,
nmap
is not so useful to receive the answer from the device. The application expects on stdout the data to be parsed. With the--data-string
I guess I can easily send the TCP payload, but I don't find an easy way to retrieve the output. -
ozzy over 5 years@Mark OK. I think I don’t understand what you are trying to achieve. I’m sorry. Perhaps it is possible to clarify the question. E.g. clarify what network entities are involved, how they are or should be configured, and which one is under your control. Otherwise, you may have more luck at SE Server Fault.
-
Mark over 5 yearsI apologize. I just trying to send a TCP packet to a remote device, receive the answer and close the socket. Of course I can write a C++ application to do the same, but I'm trying to do this on command line. The only problem is 1 second of timeout is too much...
-
ozzy over 5 years@Mark I revised my answer. It’s still not 100% clear to me what you wish to do, but I guess the ‘timeout’ tool may suit your needs.
-
Mark over 5 yearsGood hint! It seems to work as expected.
-
Gabriel Staples over 2 yearsThis is very useful for control of benchtop laboratory instruments, such as power supplies. It helped me in my Q&A here. Thanks.