Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
echo command | netcat host port

This results in the command being sent to the remote host and some data being read back. But after a few seconds, the connection closes. The -w parameter did not change anything. I am using netcat v1.10 on SuSE 10.1.

share|improve this question
Are you sure it is not the remote host that closes the connection? – grawity Mar 24 '11 at 17:18
Yes. Doing netcat and then manually typing the command results in netcat staying alive indefinitely. – Chris Mar 24 '11 at 17:40
Why would it stay alive? it just print the echo parameters and then die? – M'vy Mar 24 '11 at 17:41
I want it to stay alive so that it continues to receive the data coming from the remote server. – Chris Mar 24 '11 at 18:01
That's not true. "netcat <host> <port>" opens a bidirectional TCP socket. – Chris Mar 24 '11 at 18:41

This works with the nc command on OS X (assuming the command that you want to send is in a file):

cat file - | nc host port

(Essentially, cat dumps the contents of file on stdout and then waits for you on stdin).

By extension, if you want to send the command from the shell itself, you could do this:

cat <(echo command) - | nc host port
share|improve this answer
It works in Ubuntu 12.04 – Dmitry Mar 30 '15 at 14:18

I found it:

echo command | netcat host port -

My coworker knew it. I don't see that in the documentation at all.

share|improve this answer

With nc on Ubuntu:

nc -q -1 host port

From the Ubuntu nc man page:

 -q seconds
         after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.

Note that the available nc options vary a lot between distributions, so this might not work on yours (OpenSUSE).

share|improve this answer

I don't think you're going to manage this with either netcat or socat. I have just done extensive tinkering with both, and socat looked the most promising.

I managed to set socat up to connect to the remote TCP port and listen on a local unix domain socket (in theory so the link could be kept up all the time) but as soon as the local process detatched from the unix socket (another socat linking the unix socket to stdin/out) it closed the TCP socat session.

The problem here is that each connection through netcat / socat makes a new TCP stream connection to the server, and it closes that TCP stream session when the local end disconnects.

I think you're probably going to have to write some custom proxy software for this that opens the TCP connection to the remote end and then listens locally on a socket / pipe / fifo or whatever and then just sends the data down the existing TCP pipe and returns the results.

share|improve this answer
Listen to the all-mighty Matt Jenkins :D – M'vy Mar 24 '11 at 18:17

socat's shut-none option should help here:

Changes the (address dependent) method of shutting down the write part of a connection to not do anything.

You'll probably also need to override the default timeout period using -t <timeout>, otherwise the socket will be closed after 0.5s. This option overrides the default behaviour, which is:

When one of the streams effectively reaches EOF, the closing phase begins. Socat transfers the EOF condition to the other stream, i.e. tries to shutdown only its write stream, giving it a chance to terminate gracefully. For a defined time socat continues to transfer data in the other direction, but then closes all remaining channels and terminates.

So, a command such as:

echo 'blah' | socat -t 10 stdio tcp:<addr>:<port>,shut-none

will keep the socket open for 10s after sending the 'blah'.

share|improve this answer

Your command terminates, if either the remote host closes the connection (or isn't reachable) or the command before the pipe is terminated (while netcat still sends the rest of its input queue).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .