netcat with milliseconds interval
Asked Answered
Z

1

8

I am trying to use netcat to read a line in a file every few milliseconds, and send it to a port..

So far I know from netcat documentation that it can insert a time interval between each line sent:

This is from netcat help manual:

-i secs Delay interval for lines sent, ports scanned

I tried the following which allows me to insert a minimum of 1 second time interval between each line sent.

nc -q 10 -i 1 -lk 9999 < file_input

I would like to know if there is anyway to reduce this time interval to milliseconds. Maybe by piping input of the file to netcat using some utility that allows for configuring interval between each read in the order of milliseconds?

Zaid answered 26/10, 2015 at 3:41 Comment(0)
T
13

Using sleep from GNU coreutils allows to sleep for fractions of a second. So you can try:

while read -r line ; do echo "$line"; sleep 0.001; done < "/path/to/file" | nc host port

In each loop the variable "line" holds one line of your file, which gets sent through netcat to the host "host" on port "port". After sending one line, the code waits for 0.001 seconds, asf. until there is no more data in the file to send.

See "How do I sleep for a millisecond in bash or ksh" for more information on the ability of the sleep command to wait for fractions of a second.

Teens answered 26/10, 2015 at 23:41 Comment(2)
btw do you have any idea how I can also track how many lines have been read? i.e. if I can output to both netcat and stdoutZaid
Introduce a counter variable and echo to stderr; stdout is going already away to netcat. Something like this should work: COUNT=0; while read -r line ; do echo "$line"; let "COUNT+=1"; echo $COUNT >&2; sleep 0.001; done < "/path/to/file" | nc host portTeens

© 2022 - 2024 — McMap. All rights reserved.