readarray -t FILES < text.in
for ELEMENT in ${FILES[#]}
do
printf $ELEMENT
sleep 2
done | nc -q -1 localhost 5555
This code simulates a slow network client which sends a text line from text.in every 2 seconds to a key-value-store server with netcat.
How do I compare the output of netcat with a file test.out?
Example for text.in:
GET$5$hello
SET$5$hello$7$myworld
GET$5$hello
QUIT
Example for test.out:
ERR
OK
VALUE$7$myworld
Once the QUIT line is sent, the server closes the connection. After that I want to compare the standard output of netcat to test.out
I could do this: nc localhost 5555 < text.in | diff - test.out but then the entire file text.in is sent immediately which defeats the purpose of simulating a slow client.
I tried the following but it gets stuck and never returns anything:
readarray -t FILES < text.in
for ELEMENT in ${FILES[#]}
do
printf $ELEMENT
sleep 2
done | nc -q -1 localhost 5555 | diff - test.out
EDIT: ANSWER IN COMMENTS
Related
A very simple use of netcat is below:
#!/bin/sh
echo "hello world" > original.txt
base64 original.txt > encoded.txt
cat encoded.txt | nc -l -p 1234 -q 0
#!/bin/sh
nc localhost 1234 -q 0 > received.txt
base64 -d received.txt > decoded.txt
rm received.txt encoded.txt
echo "Sent:"
md5sum original.txt
echo "Received:"
md5sum decoded.txt
rm decoded.txt original.txt
This creates a file, encodes it into base64, sends it over netcat, and then decodes it, finally comparing whether what was sent and what was received is identical using a checksum comparison.
On a Kali Linux machine I was using earlier, the netcat connection closes upon execution of the second script, but trying on Ubuntu at home, this is not the case. I need to manually close the connection with Ctrl+D.
How can I make it such that the connection closes after receiving this data?
I think including the -c flag for nc should do it. Also, are you sure you want to use Bourne shell, and not Bash? I'd suggest changing your shebang to #!/bin/bash
I'm trying to get diagnostics data from a device to which I connect over the network. It offers a nice tight proprietary protocol where you send a 3 or 4 character request and receive a <~30 character response. Things to take into account:
You must wait 20 ms between requests (I want to poll as fast as possible)
You must wait for the response before sending the next request
While the request must end in a Unix line end, the responses come with Mac line ends (I know)
I'm trying to do this with netcat. Basic operation is easy:
$ echo STA | netcat <IP> <Port>
123498754
The STA request will give you a status word that netcat writes to stdout. I can also do this:
$ cat | netcat <IP> <Port>
STA # <- typed input
12345678 # written by netcat
STA # <- typed input
12345678 # written by netcat
Here I can see that the first request takes considerably longer: multiple seconds versus no noticeable delay. I want to get rid of the delay. And so my quest begins: Keep netcat open!
I cannot just pipe a file into netcat, because I have to wait for the responses. So I'm trying to do this using FIFOs. I created two (/d/pc2dev and /d/dev2pc), both with rw permission. I could write a script based on that if I could get the basics working. This works:
#### SHELL 1 #############################
$ tail -f /d/pc2dev | netcat <IP> <Port> | tr '\r' '\n'
12345678654 # written by netcat with delay
12345678654 # written by netcat as soon as I send the request on shell 2
12345678654 # ditto
#### SHELL 2 #############################
$ echo STA > /d/pc2dev
$ echo STA > /d/pc2dev
$ echo STA > /d/pc2dev
(without the tr, responses will get written over one another.)
However, this does not:
#### SHELL 1 #############################
$ tail -f /d/pc2dev | netcat <IP> <Port> | tr '\r' '\n' > /d/dev2pc
#### SHELL 2 #############################
$ echo STA > /d/pc2dev
$ echo STA > /d/pc2dev
#### SHELL 3 #############################
$ cat /d/dev2pc
# expecting output here - none comes!
Writing to a normal file also doesn't work:
#### SHELL 1 #############################
$ tail -f /d/pc2dev | netcat <IP> <Port> | tr '\r' '\n' > log.txt
#### SHELL 2 #############################
$ echo STA > /d/pc2dev
$ echo STA > /d/pc2dev
$ cat log.txt
# nothing
It seems that the write operation to FIFO/file is buffered somewhere, while the write to stdout is done immediately. (However, even on ctrl+c'ing netcat et al., this hypothetical buffered output does not get written.)
Be it like that or otherwise - how can I get this to work?
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
I need to sendo a lot of messages via netcat or something similar. The problem is that when I run echo "something" | netcat ip port the connection continues opened after I received the response. Actually the connection continues opened waiting for a new input. However, what I need is that the connection closed after I receive the response. Look, my script is basically this:
#!/bin/bash
i=1
while [ $i -ne 10000 ];do
sed -n $[i]p wordlist | netcat localhost 30002 >> result
i=$[$i+1]
done
If I can close the connection after print the response in result, everything will work fine. I know that there is an option -w "x" that closes the connection after "x" seconds, but the minimum value for "x" is 1 and 1 is bigger than I can wait, I need close the connection as soon as possible.
Unfortunately, the -q flag didn't work for me.
I'm using "OpenBSD netcat (Debian patchlevel 1.187-1ubuntu0.1)" and, even though the -q flag shows up in the manual, it didn't work as mentioned in cnicutar's answer.
Therefore, my workaround was:
#!/bin/sh
# POSIX COMPLIANT
HOST="localhost"
PORT=30002
scan () {
# Ensuring there is no file named msg
rm msg
# While msg file doesn't exist or is empty, do
while [ ! -s msg ]; do
# Remove instruction from within the loop
rm msg
# Append the received messages to msg file, and put the process in the background
echo "$HOST $PORT" | xargs nc >> msg &
# If the file exists and is not empty, return, we received the message
[ -s msg ] && return;
# A small timeout.. doing some tests I noticed that a timeout of zero sometimes didn't work to catch the message
# Maybe nc needs a small time to receive everything. You might want to test and increase or decrease this timeout if needed.
sleep 0.1
# This script will be spawning a lot of nc process, to kill it before the loop runs again
pkill -x nc
done
} 2> /dev/null
scan
# The function returned, so cat the file
cat msg
# make sure nc is killed
pkill -x nc > /dev/null 2>&1
rm msg
What you're looking for is the -q switch. If you specify:
netcat -q 0 localhost 30002
netcat will exit immediately.
Say I write to a netcat connection:
tail -f ${file} | nc localhost 7050 | do_whatever | nc localhost 7050
what happens here is that we have two socket connections, to do some request/response. But that's not ideal for a few reasons.
What I am looking to do is reuse the same connection, to read from and write to.
Does anyone know how I can reuse just one netcat connection?
The correct way to do this in UNIX is to make use of a back pipe. You can do so as follows:
First, create a pipe: mknod bkpipe p
This creates a file named bkpipe of type pipe.
Next, figure out what you need to do. Here are two useful scenarios. In these, replace the hosts/addresses and port numbers with the appropriate ports for your relay.
To forward data sent to a local port to a remote port on another machine:
nc -l -p 9999 0<bkpipe | nc remotehost 7000 | tee bkpipe
To connect to another machine and then relay data in that connection to another:
nc leftHost 6000 0<bkpipe | nc rightHost 6000 | tee bkpipe
If you simply need to handle basic IPC within a single host, however, you can do away with netcat completely and just use the FIFO pipe that mknod creates. If you stuff things into the FIFO with one process, they will hang out there until something else reads them out.
Yeah, I think the simplest thing to do is use this method:
tail -f ${file} | nc localhost 7050 | do_whatever > ${file}
just write back into the same file (it's a 'named pipe').
As long as your messages are less than about 500 bytes, they won't interleave.
Using ncat is much easier and understandable for beginners as a one-liner ;)
prompt$> ncat -lk 5087 -c ' while true; do read i && echo [You entered:] $i; done'
Connect with telnet (or nc) to localhost port 5087, and everything you type echoes back to you ;)
Use -lk option for listening and keeping/maintaining (multiple) connections.
You can make one bash script out of it like this, using back slashes but it invokes multiple bash, not cheap on resource usage:
#!/bin/bash
# title : ncat-listener-bidirectional.sh
# description : This script will listen for text entered by a client
# like for instance telnet
# and echo back the key strokes
#
ncat -lk 5087 -c ' \
#!/bin/bash \
while true; do \
read i && echo [You entered:] $i; \
done'
I'm running a bash script that goes through the list of my remote server IPs, connects via netcat (telnet) for each line, and runs a few commands.
The problem is I can't seem to figure out how to terminate netcat so the script can loop to the next IP in the list.
Here's the relevant bit:
#!/bin/bash
while ISF= read -r line;do
(
sleep 3
printf 'command1'
sleep 3
printf 'command2'
sleep 3
) | nc $line
done < ~/servers.txt
The remote servers don't send an EOF, so is there something I can echo or printf at netcat to terminate netcat so the script can loop through again? I would really rather not do a -w flag for a timeout, because I have quite a few servers I need to do this on, and a timeout would make it take much longer.
Specify a timeout after which nc will exit if it receives no further input, either from the remote end or via standard input.
... | nc "$line" -w 10 # Choose a value for -w as appropriate
Depends on your version of netcat, but -c should do what your looking for. From the usage statement of gnu netcat (which is likely what you're running on Ubuntu):
-c, --close close connection on EOF from stdin