Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
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
    
If you do it the way you wrote it, you just send to the remote server. If you want to receive something, you need to open a listening netcat on the local. –  M'vy Mar 24 '11 at 18:15
show 1 more comment

3 Answers

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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