7

I would like to use netcat to send a piece of text to the echo service on my server, get the reply then exit, so that I know the connection is still good. so far I've tried:

 echo 'test' | netcat server 7 

this way netcat would wait for more input rather than exit.

How can I make netcat exit after getting reply from the echo service?

5

Your command should work. It is very similar to some of the examples in the netcat manpage.

This is exactly how netcat is supposed to operate: once it has reached EOF on stdin, it (one-way) closes the connection /to/ the server and then waits for data coming from the server. When the latter closes the connection (the other way: server->client), then netcat stops.

If your netcat command doesn't finish, I suppose there is something strange happening at the network level that keeps the echo server listening for additional input.

You can try out the -q option to force netcat to stop N seconds after EOF is encountered on stdin:

netcat -q1 server echo <<< "test"

(<<< "test" is a bashism, use your echo ... | syntax if you don't use bash)

4

Use this:

cat <(echo command) - | nc host port

The problem is that nc will close the connection immediately after stdin is closed, which is very quick for a simple my_command string, and thus never gets the chance to receive a response. (If you pipe a very large file you will see that it might get a response before it's done sending the file).

Enter cat with - as second argument: It makes cat listen on stdin for more content to pipe through after it has sent the contents of the first argument. The first argument is just getting the echo command through cat – it could also be a file with your commands a la cat < file - | ....

The downside of that is now that it stays open until you hit ^C or ^D on the shell. Adding a timeout using -w 1 (OSX netcat) or -i 1 (nmap's ncat) makes it close the connection after 1 second, but it won't exit the process until you enter some character.

This answer is based on this this answer to an identical superuser question.

3

Just tried - slightly different behaviour between netcat-openbsd and netcat-traditional ( ubuntu 16.4). The OpenBSD variant does what you expect, while with the netcat-traditional I need to add the -q 1 to avoid waiting for more input.

echo 'test' | netcat -q 1 server 7
2

I don't understand your question properly?

The default netcat listener should exit upon receiving input (in this case your echo pipes it to netcat which forwards it to the listener).

If you want the listening Netcat service to stay listening after it receives input:

nc -lk 12345
  • where 12345 is the port number

Most importantly though, please check the man pages for netcat.. they are amazingly useful for all commands...

man nc
0

sending a ping to Redis and grep the PONG:

echo 'PING' | nc -w 1 server 6379 | grep PONG

Works as Ansible command.

-1
(echo 'test'; sleep 1) | netcat server 7 

This keeps the input open for some time to delay netcat ending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.