shell脚本中的netcat给出无效连接

Min*_*any 13 shell webserver netcat

我有一个 shell 脚本,用于在端口 1111 上netcat侦听localhostWeb 请求。localhost:1111/index.html例如,每次我尝试访问时,我都会得到:

invalid connection to [127.0.0.1] from localhost [127.0.0.1] 60038
Run Code Online (Sandbox Code Playgroud)

每次访问时,末尾的数字(60038)似乎都在增加localhost

关于出了什么问题的任何建议?什么是default localhost目录?我应该在哪里放一个index.html,这样localhost:1111/index.html会工作呢?

编辑

这是完整的脚本:

#!/bin/sh
while true
do
netcat -vvl localhost -p 1111 -c '
    set -x
    read http_request
    echo HTTP/1.0 200 OK
    echo
    echo "Received HTTP request: $http_request"
'   
done
Run Code Online (Sandbox Code Playgroud)

mrb*_*mrb 7

您的原始脚本要求连接来自名为 的主机localhost,但由于某种原因过滤失败。不寻常,因为它与错误中列出的名称完全匹配:invalid connection to [127.0.0.1] from localhost [127.0.0.1] 60038

此命令将侦听localhost网络接口(并忽略来自其他接口的请求,例如您的 LAN):

netcat -vvl -s localhost -p 1111 -c '
    set -x
    read http_request
    echo HTTP/1.0 200 OK
    echo
    echo "Received HTTP request: $http_request"
'
Run Code Online (Sandbox Code Playgroud)

如果要侦听所有接口上的请求,可以-s完全删除该部分:

netcat -vvl -p 1111 -c '...'
Run Code Online (Sandbox Code Playgroud)

在我的系统上,如果我想在没有 的情况下进行相同类型的源主机过滤-s,我需要使用127.0.0.1localhost.localdomain

netcat -vvl localhost.localdomain -p 1111 -c '...'

netcat -vvl 127.0.0.1 -p 1111 -c '...'
Run Code Online (Sandbox Code Playgroud)

无论如何,上述选项之一应该适合您:

$ netcat -vvl 127.0.0.1 -p 1111 -c '
quote>     set -x
quote>     read http_request
quote>     echo HTTP/1.0 200 OK
quote>     echo
quote>     echo "Received HTTP request: $http_request"
quote> '
listening on [any] 1111 ...
connect to [127.0.0.1] from localhost.localdomain [127.0.0.1] 35368
+ read http_request
+ echo HTTP/1.0 200 OK
+ echo
+ echo Received HTTP request: GET / HTTP/1.1
$
Run Code Online (Sandbox Code Playgroud)