简体   繁体   English

将输出从python脚本管道传输到远程计算机?

[英]Piping output from a python script to a remote machine?

I have a python script that output/prints a large array (50MB in size) to the screen. 我有一个python脚本,可在屏幕上输出/打印一个大数组(大小为50MB)。 This script is running on machineA . 该脚本在machineA上运行。 Is it at all possible to 'pipe' this output to machineB , where it will be processed? 是否可以将此输出“管道”到machineB ,在哪里进行处理?

I know I can save the output in a file on machineA and then send the file to machineB . 我知道我可以将输出保存在machineA上的文件中,然后将文件发送到machineB I was just wondering if it is possible to do so without having to save the data first to a file (on machineA ). 我只是想知道是否有可能这样做,而不必先将数据保存到文件(在machineA )。

Any solutions (python, bash shell, other ideas) will be appreciated. 任何解决方案(python,bash shell,其他想法)将不胜感激。

This is a work for nc (netcat)... on machineB you do 这是nc (netcat)的工作...在machineB执行

nc -l 12345 | processing_program

This command will start netcat in "listen mode" waiting for a connection. 此命令将以“侦听模式”启动netcat,以等待连接。 Once the connection is established what is coming from the network will be sent to stdout and what is sent to stdin will be sent back to whoever connected. 建立连接后,来自网络的内容将被发送到stdout,发送到stdin的内容将被发送回连接的任何人。

After that you go on machineA and run 之后,您继续在machineA运行

generating_program | nc machineB 12345

this will instruct netcat to start a connection to machineB (port 12345) and send whatever it gets from stdin over the wire. 这将指示netcat启动与machineB (端口12345)的连接,并通过电线发送从stdin获得的任何内容。 Whatever comes back from that connection is sent to standard output. 从该连接返回的所有内容都将发送到标准输出。

使用Netcat工具,您可以轻松地将数据从一台计算机传输到另一台计算机。

You can do it on different levels - here are a few options 您可以在不同级别上进行操作-这里有一些选择

  1. use ssh to pipe myprog | 使用ssh传递myprog | ssh remotemachine myotherprog ssh remotemachine myotherprog
  2. use nfs (if going to a file 使用nfs(如果转到文件
  3. use netcat (nc) 使用netcat(nc)
  4. use something like thrift 用节俭之类的东西

It depends on how solid & permanent the solution needs to be 这取决于解决方案的牢固性和持久性

Maybe something along the lines of 也许有些类似的东西

myPythonScript | ssh user@machine "sh -c 'cat >myfile.txt'"

WARNING: pulled entirely out of my head and untested! 警告:完全从我的脑海中拔出,未经测试!

On machineA: 在机器A上:

python script-on-machineA.py --options | ssh machineB tee /file/on/machine/B.txt

Alternatively, on machineB: 或者,在machineB上:

ssh machineA python /path/to/script-on-machineA.py --options | cat > /file/on/machine/B.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM