Files
rlz/测试公网连接.sh
2026-01-26 15:02:59 +08:00

77 lines
2.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 测试公网连接脚本
echo "=========================================="
echo "测试公网IP连接"
echo "=========================================="
echo ""
SERVER_IP="101.43.95.130"
PORT="8039"
echo "服务器: $SERVER_IP:$PORT"
echo ""
# 测试1: 检查端口是否可达
echo "1. 检查端口连通性..."
if timeout 3 bash -c "echo >/dev/tcp/$SERVER_IP/$PORT" 2>/dev/null; then
echo "✓ 端口 $PORT 可达"
else
echo "✗ 端口 $PORT 不可达"
echo " 可能原因:"
echo " - 云服务器安全组未配置"
echo " - 服务器本地防火墙未开放"
echo " - 网络路由问题"
fi
echo ""
# 测试2: 测试HTTP连接
echo "2. 测试HTTP连接..."
response=$(curl -s -w "\nHTTP_CODE:%{http_code}" --connect-timeout 5 \
"http://$SERVER_IP:$PORT/appLogin" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=admin123" 2>&1)
http_code=$(echo "$response" | grep "HTTP_CODE" | cut -d: -f2)
body=$(echo "$response" | sed '/HTTP_CODE/d')
if [ "$http_code" = "200" ]; then
echo "✓ HTTP连接成功 (状态码: $http_code)"
echo "响应内容:"
echo "$body" | python3 -m json.tool 2>/dev/null | head -10 || echo "$body" | head -5
elif [ -n "$http_code" ]; then
echo "⚠️ HTTP连接返回状态码: $http_code"
echo "响应内容:"
echo "$body" | head -5
else
echo "✗ 无法连接到服务器"
echo "错误信息: $response"
fi
echo ""
# 测试3: 检查服务器本地防火墙
echo "3. 检查服务器本地防火墙..."
if command -v firewall-cmd &> /dev/null; then
if sudo firewall-cmd --list-ports 2>/dev/null | grep -q "8039/tcp"; then
echo "✓ 本地防火墙已开放8039端口"
else
echo "⚠️ 本地防火墙未开放8039端口"
echo "执行以下命令开放端口:"
echo " sudo firewall-cmd --permanent --add-port=8039/tcp"
echo " sudo firewall-cmd --reload"
fi
else
echo "⚠️ 未检测到firewalld请检查iptables或其他防火墙"
fi
echo ""
echo "=========================================="
echo "总结"
echo "=========================================="
echo "如果端口不可达,请检查:"
echo "1. 云服务器控制台 → 防火墙 → 已配置8039端口规则 ✓"
echo "2. 服务器本地防火墙 → 需要开放8039端口"
echo "3. 服务器是否正在运行"
echo ""