I have a Raspberry Pi (Debian Linux) connected to my LAN that can read data from some connected devices and output it to STDOUT.

Let's say the program is run on "Server" and I want to serve the output of the script /usr/bin/data to port 1234.

I want any client to pull the output of that script by connecting to that port, using something like

nc Server 1234 > ServerData.txt

I want the server to stay alive, run the script for each connection, serve the data and then close the connection to the client. Server needs stay alive and wait for the next connection.

What is the netcat command to run on Server???


One of the more advanced versions of netcat will be required. You can use ncat or socat:

Server Side

When client connects, execute the executable program /usr/bin/data and send program output to client.

NCAT Method:

$ ncat -l 1234 -c '/usr/bin/data' --keep-open

SOCAT Method

$ socat -U TCP-LISTEN:5403,fork EXEC:'/usr/bin/data',stderr,pty,echo=0

Client Side:

Connect to the server, and receive output from server.

NCAT Method to File

$ ncat Server 1234 | tee ServerData.txt

NC (netcat) Method to stdout

$ </dev/null nc <Server.ip> 5403 > /dev/stdout

to save to a file replace /dev/stdout with a file name

WGET Method to File

$ wget -q <Server.ip>:5403 -O myfile.txt

to print results replace myfile.txt with /dev/stdout

  • 1
    Looks fine, except the OP wants to execute /usr/bin/data, not send it. I think it should be -c /usr/bin/data or -e /usr/bin/data. Mar 18 '20 at 21:09
  • Use ncat developed by nmap project, it is more powerful than old nc.
    – lukascode
    Mar 18 '20 at 22:00
  • @Joshua Besneatte sure
    – lukascode
    Mar 19 '20 at 7:50

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.