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

谁都会走 提交于 2019-12-10 17:37:58

问题


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.


回答1:


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.




回答2:


Perfect answer, with one slight modification: adding -p for "port"

Receiver:

nc -l -p 8011 >logfile.txt

I pulled in the nc help file ( nc -h) using your suggestion

Sender:

nc -h 2>&1 | nc 127.0.0.1 8011

I tried

nc -h > logfile.txt
nc -h >> logfile.txt

which never worked.

I'm running netcat for Windows 7 in 2 cmd.exe's

Thanks, again



来源:https://stackoverflow.com/questions/7939023/is-there-a-way-to-send-a-continuous-stream-from-a-command-output-to-a-remote-lis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!