我需要 netcat 来监听传入的 HTTP 请求,并且根据请求,我需要运行一个脚本。
到目前为止,我有这个;
netcat -lk 12345 | grep "Keep-Alive"
所以每次 netcat 收到一个包含“keep-alive”的包时,我都需要触发一个脚本。
它需要在 crontab 中运行...
谢谢你的帮助!
这个怎么样?
#!/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”命令替换为您要执行的脚本。
怎么样:
#!/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.
根据您在客户端的情况,它可能会坐在那里等待 netcat/服务器响应。
我做了与上述类似的事情,但使用了
while true do
netcat -l 1234 < 404file.txt
找不到文件404file.txt
在哪里HTTP/1.1 404
这会断开客户端的连接,并且由于netcat没有“k”,因此它会终止并重新启动,因为 while true 已准备好再次接收和发送 404。