假设我向一个Netcat连接发送信息:
tail -f ${file} | nc localhost 7050 | do_whatever | nc localhost 7050
这里发生的情况是我们有两个socket连接,用于进行一些请求和响应。但这并不是理想的做法,原因有几个。
我想要做的是重用同一个连接,以进行读取和写入操作。
有人知道如何只重用一个netcat连接吗?
mknod bkpipe p
这将创建一个名为bkpipe
的文件,类型为管道。 nc -l -p 9999 0<bkpipe | nc remotehost 7000 | tee bkpipe
连接到另一台计算机,然后将该连接中的数据中继到另一台计算机:
nc leftHost 6000 0<bkpipe | nc rightHost 6000 | tee bkpipe
是的,我认为最简单的方法是使用这种方式:
tail -f ${file} | nc localhost 7050 | do_whatever > ${file}
请将内容写回到同一文件中(它是一个“命名管道”)。
只要您的消息小于约500字节,它们就不会交错。
tail -f
正在读取的文件。 - Barmar#!/bin/bash
# title : ncat-listener-bidirectional.sh
# description : This script will listen for text entered by a client
# like for instance telnet
# and echo back the key strokes
#
ncat -lk 5087 -c ' \
#!/bin/bash \
while true; do \
read i && echo [You entered:] $i; \
done'
do_whatever
收到某个消息,它将使用第二个连接向TCP服务器回写。主要问题是当其他进程想要与此进程通信时,他们可能会看到两个连接而不是一个来进行通信。 - Alexander Mills