Search code examples
bashsocketsshellunixnetcat

Is there a way to send a continuous stream from a command output to a remote listener


I'm using the netcat for unix.

when I run python script.py &> logfile.txt , it gets captured continuously.

To replicate this remotely, I tried nc -l -p 8011 on the listener (client) and the following for the sender (host or server) :

  1. python script.py &> nc 127.0.0.1 8011
  2. python script.py > nc 127.0.0.1 8011
  3. nc 127.0.0.1 8011 < python script.py

But nothing seems to work. Please help.


Solution

  • Is this what you're after?

    Receiver:

    nc -l 8011 >logfile.txt
    

    Sender:

    python script.py 2>&1 | nc 127.0.0.1 8011
    

    Make sure to run the receiver code first.


    EDIT: In case you're not aware there's a lot of different versions of netcat; they all accept slightly different arguments (e.g. nc.traditional on Debian wants nc -l -p 1234 to listen on port 1234, whereas BSD nc (e.g. OS X) just wants nc -l 1234 and ncat may throw an interesting error unless you use the -4 flag if your host doesn't support IPv6) - read the man pages to find out what combination of options you actually want.