如何通过 netcat 和 fifo 将 2 个远程进程连接在一起?

发布于 2024-12-25 20:47:19 字数 2083 浏览 1 评论 0原文

在 HostA 上,我有一个执行 I/O 的进程 procA,如下所示:

#!/bin/bash
while sleep 1 ; do
  # Consume input *if available*
  if read -t 0.5 x; then  # a timed-read
    # If input is available, output it.
    echo "HostA Input: $x"
  fi

  # Produce some output.
  echo "HostA Output: $(date)"
done

在 HostB 上,我有一个非常相似的进程 procB,唯一的区别是procB 使用前缀 HostB(而不是前缀 HostA)标记其输入/输出。

现在,我想使用 netcat 程序 (/usr/bin/nc) 将 procA 的标准输出发送到 procB 的标准输入,以及 procB 的标准输出到 procA 的标准输入。我对这一切的期望是 HostA 的输出应该出现在 HostB 上,HostB 的输出应该出现在 HostA 上...就像下面的对等点的规范 nc 示例中发生的情况一样-对等聊天:

[HostB]$ nc -l 4000

[HostA]$ nc HostB 4000

但是,当我尝试通过用进程 procAprocB 替换两台主机上的人员来扩展上述规范示例时,如下所示,不起作用

HostB

$ mkfifo p
$ ./procB <p | nc -l 4000 >p
<WHY NO OUTPUT HERE??>

并且在 HostA 上:

$ mkfifo p
$ ./procA <p | nc HostB 4000 >p
<WHY NO OUTPUT HERE??>

注释:

  1. 我已验证 HostB 上的端口 4000 已打开。
  2. 单向通信,如下所示,似乎有效

HostB 上:

$ mkfifo p
$ ./procB <p | nc -l 4000 >p

HostA 上:

$ ./procA | nc HostB 4000 
HostB Output: Wed Jan 11 13:53:37 IST 2012
HostB Output: Wed Jan 11 13:53:39 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:02 IST 2012
HostB Output: Wed Jan 11 13:53:40 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:04 IST 2012
HostB Output: Wed Jan 11 13:53:41 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:05 IST 2012
HostB Output: Wed Jan 11 13:53:42 IST 2012
^C

现在,我知道了nc 带有一个不安全选项 -e,如果在您的 nc 二进制文件中启用了该选项,则可以以某种方式使用该选项,但假设我没有有 -e 选项可用,我该如何通过 nc 连接 2 个远程进程?基本上,我上面哪里出错了?为什么我上面指出的地方没有输出?非常感谢。

On HostA, I have a process, procA, that does I/O, like so:

#!/bin/bash
while sleep 1 ; do
  # Consume input *if available*
  if read -t 0.5 x; then  # a timed-read
    # If input is available, output it.
    echo "HostA Input: $x"
  fi

  # Produce some output.
  echo "HostA Output: $(date)"
done

On HostB, I have a very similar process, procB, with the only difference that procB tags its input/output with the prefix HostB (instead of the prefix HostA).

Now, I want to use the netcat program (/usr/bin/nc) to send procA's stdout to procB's stdin, and procB's stdout to procA's stdin. My expectation in all this is that HostA's output should appear on HostB, and HostB's output should appear on HostA... much as would happen in following canonical nc example of a peer-to-peer chat:

[HostB]$ nc -l 4000

[HostA]$ nc HostB 4000

But, when I tried to extend the above canonical example by replacing the human at both hosts with processes procA and procB as follows, it did not work:

On HostB:

$ mkfifo p
$ ./procB <p | nc -l 4000 >p
<WHY NO OUTPUT HERE??>

and, on HostA:

$ mkfifo p
$ ./procA <p | nc HostB 4000 >p
<WHY NO OUTPUT HERE??>

Notes:

  1. I have verified that port 4000 is open on HostB.
  2. Unidirectional communication, such as the following, seems to work:

On HostB:

$ mkfifo p
$ ./procB <p | nc -l 4000 >p

On HostA:

$ ./procA | nc HostB 4000 
HostB Output: Wed Jan 11 13:53:37 IST 2012
HostB Output: Wed Jan 11 13:53:39 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:02 IST 2012
HostB Output: Wed Jan 11 13:53:40 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:04 IST 2012
HostB Output: Wed Jan 11 13:53:41 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:05 IST 2012
HostB Output: Wed Jan 11 13:53:42 IST 2012
^C

Now, I know that nc comes with an INSECURE option -e that could somehow be employed if it's enabled in your nc binary, but assuming that I don't have -e option available, how can I connect 2 remote processes to each other via nc? Basically, where I'm going wrong above? Why there is no output in the places I have indicated above? Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2025-01-01 20:47:19

上述行为实际上是正确的行为,只是我没有意识到它是正确的。

无论我在哪里抱怨 <为什么这里没有输出?>,都是因为终端的标准输出已被重定向到本地 fifo。 (难怪,你在终端上看不到任何东西!)

此外,在上面的注释(2)中,通信是单向的并不真实;相反,通信是单向的。它是非常双向的,只是您在 HostB 上看不到任何输出,因为 stdin 和 stdout 已从终端重定向到 fifo。因此,您正确看到 HostA 上 procB 的输出,这只不过是 HostA 上 procA 发送到 procB< 的输出。 HostB 上的 /code> 作为 procB 的输入!

哇!

编辑:
要实际查看每个终端上的输出,请使用 tee,如下所示:

On HostB:

$ mkfifo p
$ ./procB <p | nc -l 4000 | tee p

On HostA:

$ mkfifo p
$ ./procA <p | nc HostB 4000 | tee p

The behavior described above is actually the right behavior, just that I did not realize that it was.

Wherever I was complaining about <WHY NO OUTPUT HERE??>, it was/is because the stdout's of the terminals have been redirected to the local fifos. (No wonder, you don't see anything on the terminal!)

Further, in Notes (2) above, it is not really true that the communication is unidirectional; it is very much bidirectional, just that you don't get to see any output on HostB, once again, because stdin and stdout have redirected from the terminal to the fifo. Thus, you correctly see procB's output on HostA, which is nothing but the output of procA on HostA sent to procB on HostB as procB's input!

Wow!

EDIT:
To actually see the output on each terminal, use tee, like so:

On HostB:

$ mkfifo p
$ ./procB <p | nc -l 4000 | tee p

On HostA:

$ mkfifo p
$ ./procA <p | nc HostB 4000 | tee p
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文