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?
nc and netcat are two names for the same program (typically, one will be a symlink to the other). Though—for plenty of confusion—there are two different implementations of Netcat ("traditional" and "OpenBSD"), and they take different options and have different features. Ncat is the same idea, but from the Nmap project.
Ncat is a feature-packed networking utility which reads and writes data across networks from the command line. Ncat was written for the Nmap Project and is the culmination of the currently splintered family of Netcat incarnations.
The command nc –l will put Netcat into server or listening mode, and nc by itself will run Netcat in client mode.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With