使用netcat作为简单的CGI服务器

发布于 2024-12-02 21:22:07 字数 810 浏览 0 评论 0原文

我试图使用以下脚本创建一个简单的 CGI 服务器(来自 http ://www.jfranken.de/homepages/johannes/vortraege/netcat_inhalt.en.html)。这更多的是关于它不起作用的技术原因,而不是这是否是一个好主意:

#!/bin/bash
export port=${port:-$1} # inherit $1 as $port
/usr/local/bin/nc -l -p $port -e $0 &  # await further connections on this port
echo "hi"

有人知道为什么这不能正常工作吗?第一次执行时,它会在终端上打印“hi”,这是我所期望的。然后,我希望 netcat 在指定的端口上等待,准备生成该脚本的新副本,其中包含与网络套接字关联的 STDOUT(由于 -e 标志)。因此,对指定端口的 HTTP GET 请求将收到文本“hi”。

实际发生的情况是,每个 GET 请求都会导致生成一个新的 netcat 进程,一次一个(这是我所期望的),但在我杀死最新的 netcat 进程之前,所有 GET 请求都不会完成。此时,我发出的第一个 GET 请求以“hi”响应,所有其他请求都以失败的连接尝试返回。我怀疑这与我对 STDOUT 重定向细节的困惑有关,但我的理解是,每次有人连接时,该脚本都会生成一个新版本,并且该脚本的 STDIN/OUT 应该是网络套接字因为 -e netcat 标志。

I was trying to use the following script to create a simple CGI server (from http://www.jfranken.de/homepages/johannes/vortraege/netcat_inhalt.en.html). This more about the technical reason it's not working than whether this is a good idea:

#!/bin/bash
export port=${port:-$1} # inherit $1 as $port
/usr/local/bin/nc -l -p $port -e $0 &  # await further connections on this port
echo "hi"

Does anyone know why this doesn't work properly? Upon first execution, it prints "hi" to the terminal, which I expect. I would then expect netcat to be waiting on the specified port, ready to spawn a new copy of this script with the STDOUT associated with the network socket (because of the -e flag). So HTTP GET requests to the specified port would then receive the text "hi".

What actually happens is that each GET request causes a new netcat process to be spawned, one at a time (which I expect), but none of the GET requests complete until I kill the latest netcat process. At that point, the first GET request I issued responds with "hi", and all the others come back as failed connection attempts. I suspect this has something to do with my confusion about the details of STDOUT redirection, but my understanding is that this script would just spawn a new version of itself every time someone connected, and the STDIN/OUT for the script should be the network socket because of the -e netcat flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2024-12-09 21:22:07

除非使用 -DGAPING_SECURITY_HOLE 编译,否则 netcat 的 -e 选项将不起作用,这应该会让您暂停一下以这种方式做事是否明智。这也是一个仅在 Hobbit 的 netcat 和 bsd-ish 衍生品中可用的选项,据我所知,在 GNU netcat 中不可用。

通过将数据传输到 netcat 进程中,您能否获得所需的结果?例如,

echo "hi" | nc -l -p $port 

似乎具有大致相同的效果,但是数据可能是在建立连接之前生成的,而不是像使用 -e 标志那样在连接之后生成。如果您的数据不需要基于时间或交互式,那么这将起作用。

The -e option to netcat will not work unless it is compiled with -DGAPING_SECURITY_HOLE which should give you some pause as to the wisdom of doing things this way. It's also an option that is only available in Hobbit's netcat and bsd-ish derivatives, not afaik in GNU netcat.

Could you achieve the desired result by piping your data into the netcat process? For example

echo "hi" | nc -l -p $port 

Seems to have largely the same effect, however the data is likely generated before the connection is made, rather than after as in using the -e flag. If your data doesn't need to be time-based or interactive this would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文