2

我需要 netcat 来监听传入的 HTTP 请求,并且根据请求,我需要运行一个脚本。

到目前为止,我有这个;

netcat -lk 12345 | grep "Keep-Alive"

所以每次 netcat 收到一个包含“keep-alive”的包时,我都需要触发一个脚本。

它需要在 crontab 中运行...

谢谢你的帮助!

4

3 回答 3

8

这个怎么样?

    #!/bin/bash

    netcat -lk -p 12345 | while read line
    do
        match=$(echo $line | grep -c 'Keep-Alive')
        if [ $match -eq 1 ]; then
            echo "Here run whatever you want..."
        fi
    done

将“echo”命令替换为您要执行的脚本。

于 2013-08-01T09:58:48.597 回答
2

怎么样:

#!/bin/bash
netcat -lk -p 12345 | grep 'Keep-Alive' | while read unused; do
    echo "Here run whatever you want..."
done

或者

#!/bin/bash
netcat -lk -p 12345 | sed -n '/Keep-Alive/s/.*/found/p' | xargs -n 1 -I {} do_something
# replace do_something by your command.
于 2013-08-01T12:01:44.627 回答
0

根据您在客户端的情况,它可能会坐在那里等待 netcat/服务器响应。

我做了与上述类似的事情,但使用了

while true do
   netcat -l 1234 < 404file.txt

找不到文件404file.txt在哪里HTTP/1.1 404

这会断开客户端的连接,并且由于netcat没有“k”,因此它会终止并重新启动,因为 while true 已准备好再次接收和发送 404。

于 2019-12-17T17:58:41.107 回答