bash 处理通过管道传输到 netcat 的脚本的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10654586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling input from script piped to netcat
提问by Grambot
I have a system that handles incoming emails to send them to a blackbox application at my job. The high level script is maintained by inittab to always run and runs a child script to do the actual work with this command:
我有一个系统可以处理传入的电子邮件,将它们发送到我工作中的黑盒应用程序。高级脚本由 inittab 维护以始终运行并运行子脚本以使用此命令执行实际工作:
$SCRIPT | nc -l -p $PORT
The script itself reads from a named pipe, does a bit of parsing and processing of the data before calling echoto shuffle the data back through netcat to the process connected on $PORT.
脚本本身从命名管道中读取,在调用echo通过 netcat 将数据混洗回 $PORT 上连接的进程之前,对数据进行一些解析和处理。
What I need is some method to handle incoming data from the far end of my pipe. When I make a request within the application to close the connection it sends back a string (I can define it to whatever I want) and waits for my script to close the pipe. I currently am struggling to understand how I can add in the functionality to read incoming data from the other end; verify it is the command to close the pipe, and then exit the script.
我需要的是某种方法来处理来自管道远端的传入数据。当我在应用程序中发出关闭连接的请求时,它会发回一个字符串(我可以将其定义为任何我想要的)并等待我的脚本关闭管道。我目前正在努力理解如何添加从另一端读取传入数据的功能;验证它是关闭管道的命令,然后退出脚本。
My script (in a nutshell) looks like this:
我的脚本(简而言之)如下所示:
while true ; do
email_input="`cat "$pipe"`"
if [[ $email_input =~ .*escape_queue.* ]] ; then
break;
fi
echo "`parse`"
done
I'm open to the possibility of having to alter the program flow, I just can't wrap my head around how I would be able to read the data incoming asynchronously since the script blocks on cat $pipeuntil a new email is received to process.
我对不得不改变程序流程的可能性持开放态度,我无法理解如何能够异步读取传入的数据,因为脚本会cat $pipe一直阻塞,直到收到要处理的新电子邮件。
If its not clear, I'm at a novice level with bash scripting and am always open to suggestions for improvement.
如果不清楚,我在 bash 脚本方面处于新手级别,并且总是乐于接受改进建议。
UPDATEI've changed my script call to
更新我已将脚本调用更改为
$SCRIPT | nc -l -p $PORT > $nc_data
and within the script itself
并在脚本本身内
netcat_response="`cat "$nc_data"`";
if [[ "$netcat_response" =~ "exit" ]] ; then
cat /dev/null > $nc_data
break;
fi
At this point the script terminates once a new message is piped into the fifo. This means that I will always lose 1 message as it gets read by the script and then the script terminates. The script still blocks on the catuntil something is read. Worst case scenario this will have to do.
此时,一旦新消息通过管道传输到 fifo 中,脚本就会终止。这意味着我将始终丢失 1 条消息,因为它被脚本读取,然后脚本终止。在cat读取某些内容之前,脚本仍会阻塞。最坏的情况是必须这样做。
采纳答案by staticsafe
You can have nc quit after a certain time from the EOF of stdin.
您可以让 nc 从标准输入的 EOF 一段时间后退出。
$SCRIPT | nc -l -q 5 -p $PORT > $nc_data
-qbeing the option to quit after a certain amount of seconds.
-q是在特定秒数后退出的选项。

