如何延迟管道Netcat在第一个输入上进行连接 [英] How to delay pipe netcat to connect on first input

查看:83
本文介绍了如何延迟管道Netcat在第一个输入上进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ubuntu下以bash运行:

Running in bash under Ubuntu:

我有一个可以为我带来一些输出的信号源,但不是立即产生的.假设这是第一个在套接字netcat -l 12345上监听的netcat.

I have a source that generates me some output, but not straight away. Let's assume it is a first netcat listening on a socket: netcat -l 12345.

我想将其通过管道传送到外发的netcat(通过TCP连接),例如netcat -l 12345 | netcat localhost 54321.但是棘手的一点是,我知道当我运行命令时,没有人监听本地主机54321上的传入连接,但是我知道当第一个实际字符通过管道到达时,就会有一个连接.

And I would like to pipe it to an outgoing netcat (connecting over TCP), e.g. netcat -l 12345 | netcat localhost 54321. But the tricky bit is, that I know there is nothing listening for that incoming connection on localhost 54321 when I run the command, but I know there will be one when the first actual character arrives through the pipe.

所以我的问题是:有没有办法:

So my question is: is there a way either:

  • 将执行外出的netcat的执行延迟到第一个字符到达管道之前,或者
  • 要延迟外发Netcat尝试建立TCP连接,直到它在其标准输入上收到第一个字符? (对于这种情况,没有直接的选择,不能切换到UDP)

提前谢谢!

实际上,源代码比netcat更复杂,也就是说,它是通过各种流修改管道传递的侦听netcat.

In reality, the source is more complex than a netcat, namely it is a listening netcat piped through all sort of stream modification.

推荐答案

使用您已经做过且我评论过的研究(不知道这是对您自己问题的回答),以下是完整的delayed_netcat.sh:

Using the research you already did and that I commented to (by not knowing it was an answer to your own question), here is the full delayed_netcat.sh:

#!/bin/bash
read line
netcat "${@}" < <(echo $line ; cat)

这首先等待一行输入,然后使用简单的echo在该行的前面添加到实际的netcat的新生成的"输入中.其余的stdin只是使用cat重定向,它会将其从stdin中删除,并将其添加到netcat的输入中.它还支持将命令行选项和参数传递给真实" netcat.

This first waits for a line of input and later prepends that line using a simple echo to the "newly generated" input to the actual netcat. The rest of stdin is just redirected using cat which slurps it from stdin and adds it to the input of netcat. It also supports passing commandline options and arguments to the "real" netcat.

用法如下:

netcat -l 12345 | cmd1 | cmd2 | ... | ./delayed_netcat.sh localhost 54321

netcat延迟到读取第一行.如果您真的想在读取第一个字符后启动它,则需要重写带有readecho的部分.

The netcat is delayed till the first line is read. If you really want to start it after the first character is read the parts with read and echo need some rewrite.

这篇关于如何延迟管道Netcat在第一个输入上进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆