我将ncat用作bash编写的简单网页的Web服务器。但是如何从当前请求中获取get和post请求参数以及客户端ip?我尝试将标准输出和错误管道重定向到日志文件。但这是有问题的,因为我总是只从前一个请求中获取日志,而不是当前请求。
这里是我的代码:终端命令:
while true; do bash indexlog.sh | ncat -lv 8000 &>>ncat.log;done
cat indexlog.sh:
#!/bin/bash
echo -e 'HTTP/1.1 200 OK\r\n'
echo -e '<!DOCTYPE html>'
echo -e '<html lang="en">'
echo -e '<head>'
echo -e '<meta charset="utf-8"/>'
echo -e '</head>'
echo -e '<body>'
echo -e '<pre>'
echo request time=$(date)
cat ncat.log
echo ''>ncat.log
echo -e '</pre>'
echo -e '</body>'
您的脚本运行并输出到管道。然后开始监听ncat
。尝试将侦听器作为单独的进程运行。首先启动侦听器。
# listener process: (`-k` to keepalive)
ncat -lk 8000 >> ncat.log
然后在另一个终端中:
# incoming requests
while true; do
./indexlog.sh | ncat localhost 8000
sleep 5
done
编辑还要删除这些行,这可能行得通,但可能会导致冲突,因为由于截断,一个进程不得不重新寻找日志的开头-:
cat ncat.log
echo ''>ncat.log
现在有了ncat + lua脚本,它又回来了。看起来像对象引用左右:端子1:
user1@pc1:~/ncattest$ ncat -klv 8000 --lua-exec hello.lua
端子2:
user1@pc1:~/ncattest$ cat hello.lua
#!/usr/bin/lua
print(os.getenv("QUERY_STRING"))
user1@pc1:~/ncattest$ curl localhost:8000/a
curl: (52) Empty reply from server
user1@pc1:~/ncattest$ curl localhost:8000/?a=3&g=5
[1] 20312
user1@pc1:~/ncattest$ curl: (52) Empty reply from server
[1]+ Exit 52 curl localhost:8000/?a=3
user1@pc1:~/ncattest$ curl localhost:8000/?a=3&g=5
[1] 20317
user1@pc1:~/ncattest$ curl: (52) Empty reply from server
[1]+ Exit 52 curl localhost:8000/?a=3
user1@pc1:~/ncattest$ curl localhost:8000/a
curl: (52) Empty reply from server
user1@pc1:~/ncattest$ curl localhost:8000/a?b=3&c=5
[1] 20329
user1@pc1:~/ncattest$ curl: (52) Empty reply from server
[1]+ Exit 52 curl localhost:8000/a?b=3
user1@pc1:~/ncattest$
但是这无济于事。问题仍然是,在纯ncat环境中没有query_string环境变量。因此没有可用的动态网页。因此,如果我想使用bash进行真正的Web脚本编写,则需要继续使用cgi脚本编写...因此,在此ncat bash级别上获取查询字符串的唯一方法是,首先尝试重定向bash的stdout和stderr。但这对我不起作用。