bash 有没有办法将连续流从命令输出发送到远程侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7939023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:00:33  来源:igfitidea点击:

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

bashsocketsshellunixnetcat

提问by Sushant Khurana

I'm using the netcat for unix.

我正在为 unix 使用 netcat。

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

当我运行时python script.py &> logfile.txt,它会不断被捕获。

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

为了远程复制这个,我尝试nc -l -p 8011了侦听器(客户端)和发件人(主机或服务器)的以下内容:

  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
  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.

但似乎没有任何效果。请帮忙。

回答by Benjie

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.traditionalon Debian wants nc -l -p 1234to listen on port 1234, whereas BSD nc(e.g. OS X) just wants nc -l 1234and ncatmay throw an interesting error unless you use the -4flag if your host doesn't support IPv6) - read the man pages to find out what combination of options you actually want.

编辑:如果您不知道有很多不同版本的 netcat;它们都接受略有不同的参数(例如nc.traditional在 Debian 上想要nc -l -p 1234监听端口 1234,而 BSD nc(例如 OS X)只是想要nc -l 1234并且ncat可能会抛出一个有趣的错误,除非您在-4主机不支持 IPv6 的情况下使用该标志) - 阅读 man页面以找出您真正想要的选项组合。

回答by Chris Hagelstein Clark U

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

完美答案,稍加修改:为“端口”添加-p

Receiver:

接收者:

nc -l -p 8011 >logfile.txt

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

我使用您的建议提取了 nc 帮助文件 (nc -h)

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

我正在 2 个 cmd.exe 中为 Windows 7 运行 netcat

Thanks, again

再次感谢