通过 SSH 从多个远程主机收集 STDOUT 时的数据完整性问题
假设您运行以下命令:
ssh $host1 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' > /tmp/output ssh $host2 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' >> /tmp/output ssh $host3 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' >> /tmp/output
那么输出将如下所示:
Hello from host1 Hello from host2 Hello from host3 Hello from host1 ...
但是如果我将其更改为
ssh $host1 'while [ 1 ]; do sleep 1; cat /some/large/file1.txt; done' > /tmp/output ssh $host2 'while [ 1 ]; do sleep 1; cat /some/large/file2.txt; done' >> /tmp/output ssh $host3 'while [ 1 ]; do sleep 1; cat /some/large/file3.txt; done' >> /tmp/output
使来自每个主机的标准输出不适合单个缓冲区会怎么样?请问数据 在这种情况下,要保持文件[1-3].txt 的完整性,而不是顺序吗?是 其他文件的文件片段可能会滑到中间 像这样的其他文件?
[file1_fragment1] [file2_fragment1] [file1_fragment2] [file1_fragment3] [file3_fragment1] ...
Suppose you run the following commands:
ssh $host1 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' > /tmp/output ssh $host2 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' >> /tmp/output ssh $host3 'while [ 1 ]; do sleep 1; echo "Hello from $HOSTNAME"; done' >> /tmp/output
Then the output would look like:
Hello from host1 Hello from host2 Hello from host3 Hello from host1 ...
But what if I changed it to
ssh $host1 'while [ 1 ]; do sleep 1; cat /some/large/file1.txt; done' > /tmp/output ssh $host2 'while [ 1 ]; do sleep 1; cat /some/large/file2.txt; done' >> /tmp/output ssh $host3 'while [ 1 ]; do sleep 1; cat /some/large/file3.txt; done' >> /tmp/output
so that stdout from each host won't fit into a single buffer? Would the data
integrity of file[1-3].txt, and not the order, be maintained in this case? Is
there a possibility that a file fragment of some other file slips in the middle
of some other file like this?
[file1_fragment1] [file2_fragment1] [file1_fragment2] [file1_fragment3] [file3_fragment1] ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,发生这种情况的可能性几乎是 100% ;-) 假设通过网络
cat
一个文件所需的时间很长。数据将按照与接收顺序大致相同的顺序写入本地系统上的
/tmp/output
。 shell 不知道要保留来自 ssh 命令 #2 或 #3 的数据,直到 #1 出现中断,此外,它也不知道文件 1 的每次迭代的结尾在哪里。I would say the possibility of that happening is pretty much 100% ;-) assuming the time taken to
cat
one file over the network is long.The data will be written to
/tmp/output
on the local system in approximately the same order that it is received. The shell doesn't know to hold on to data that comes from ssh command #2 or #3 until there's a break in #1, and besides, it will have no idea where the end of each iteration of file 1 comes.