我想为 netcat 服务器创建两个管道,将文本输入到服务器中,另一个管道从服务器输出文本。我让服务器在带有输入和输出管道的单独脚本中运行。另一个脚本(我还没有制作)使用输入管道读取 netcat 服务器的输出,然后将数据发送到输出管道。在发生这种情况的整个过程中,客户端正在向服务器发送数据,而我没有编写的脚本会读取服务器上从客户端发送的数据,然后将数据输出到服务器,服务器再发送到客户端。当我添加输出或输入管道时,服务器脚本不会从客户端接收数据,并且它不会响应发送到输入管道的数据。我不知道为什么会发生这种情况,所以如果有人能告诉我如何做到这一点,那就太好了。
服务器脚本:
#!/bin/bash
mkfifo in
mkfifo out
sudo nc -lk 22 <in >out
#Yes, I know I can run this in background, but I don't
#+want to do that yet.
客户端脚本:
#!/bin/bash
while [ 1 ]
do
nc localhost 22
done
#I know this block looks odd, but it allows the client to continue
#+communicating with the server, even after an EOF.
理论服务器处理脚本:
#!/bin/bash
#This script is run after the server script is run in another console.
while [ 1 ]
do
in="$(cat out)" #The script will be stuck here until there is output from the "out" pipe
if [ $in = "blah" ]
then
echo "blah to you, too." >in #Here, the echo output should be sent to the client from the server
fi
#I can add a lot more here after I get this sorted out
done