获取 Netcat 响应

获取 Netcat 响应

Netcat 正在用于在端口 :1000 上创建监听器

while true;    do   echo -e "HTTP/1.1 200 OK\n\n $(date)"  |  nc -l -p 1000 -q 1;    done;

在浏览器中导航到X.X.X.X:1000,或通过 curl 命令请求该响应将按预期返回日期。

当使用 javascript fetch 时,它会返回一个空响应:

fetch('http://144.126.251.93:1500', 
        {
            mode: 'no-cors',
            method: 'GET',
                headers: {
                    'Access-Control-Allow-Origin': '*'  
                }
            }
        )
        .then(response => {
            console.log( response );
        })
        .then(data => console.log(data))
        .catch(error => console.error('Fetch error:', error)
);
Response {type: 'opaque', url: '', redirected: false, status: 0, ok: false, …}
body
: 
null
bodyUsed
: 
false
headers
: 
Headers {}
ok
: 
false
redirected
: 
false
status
: 
0
statusText
: 
""
type
: 
"opaque"
url
: 
""
[[Prototype]]
: 
Response

尝试在 netcat 响应中添加GET /之前以及添加到获取标头之后,仍未得到解决。HTTP/1.1Content-Type

需要向 netcat 响应和/或获取请求添加哪些标头等?

答案1


我会使用 postman 来调试 https://www.postman.com/

行业领先的网络调试工具

#!/

#!/bin/bash

# Set the variables for your collection and environment files
COLLECTION_FILE="netcat_listener_collection.json"
ENVIRONMENT_FILE="netcat_listener_environment.json"
RESPONSE_FILE="netcat_listener_response.txt"

# Create the Postman collection JSON
cat <<EOF > $COLLECTION_FILE
{
    "info": {
        "name": "Netcat Listener Testing",
        "_postman_id": "unique-postman-id",
        "description": "Collection to test netcat listener functionality"
    },
    "item": [
        {
            "name": "Send Message to Netcat Listener",
            "request": {
                "method": "POST",
                "header": [],
                "body": {
                    "mode": "raw",
                    "raw": "Hello from Postman!"
                },
                "url": {
                    "raw": "http://your_netcat_listener_address:port",
                    "protocol": "http",
                    "host": [
                        "your_netcat_listener_address"
                    ],
                    "port": "port",
                    "path": []
                }
            },
            "response": []
        }
    ]
}
EOF

# Check if Newman is installed
if ! command -v newman &> /dev/null; then
    echo "Newman is not installed. Please install Newman: npm install -g newman"
    exit 1
fi

# Check if the collection file exists
if [ ! -f "$COLLECTION_FILE" ]; then
    echo "Collection file '$COLLECTION_FILE' not found."
    exit 1
fi

# Check if the environment file exists
if [ ! -f "$ENVIRONMENT_FILE" ]; then
    echo "Environment file '$ENVIRONMENT_FILE' not found."
    exit 1
fi

# Run Newman with the collection and environment files, and save the response to a file
newman run "$COLLECTION_FILE" -e "$ENVIRONMENT_FILE" >> "$RESPONSE_FILE"

echo "Response saved to $RESPONSE_FILE"

为了让 netcat 提供 HTML 服务,你需要执行以下操作printf 'HTTP/1.1 200 OK\n\n%s' "$(cat index.html)" | netcat -l 8888

相关内容