|
@@ -0,0 +1,173 @@
|
|
|
+#!/bin/bash
|
|
|
+
|
|
|
+# RTSP网络连接诊断脚本
|
|
|
+
|
|
|
+set -e
|
|
|
+
|
|
|
+# 颜色定义
|
|
|
+RED='\033[0;31m'
|
|
|
+GREEN='\033[0;32m'
|
|
|
+YELLOW='\033[1;33m'
|
|
|
+NC='\033[0m' # No Color
|
|
|
+
|
|
|
+print_info() {
|
|
|
+ echo -e "${GREEN}[INFO]${NC} $1"
|
|
|
+}
|
|
|
+
|
|
|
+print_warn() {
|
|
|
+ echo -e "${YELLOW}[WARN]${NC} $1"
|
|
|
+}
|
|
|
+
|
|
|
+print_error() {
|
|
|
+ echo -e "${RED}[ERROR]${NC} $1"
|
|
|
+}
|
|
|
+
|
|
|
+# 从配置文件读取RTSP URLs
|
|
|
+get_rtsp_urls() {
|
|
|
+ if [[ -f "config.json" ]]; then
|
|
|
+ # 使用jq解析JSON(如果可用)
|
|
|
+ if command -v jq &> /dev/null; then
|
|
|
+ jq -r '.streams[].rtsp_url' config.json
|
|
|
+ else
|
|
|
+ # 简单的grep解析
|
|
|
+ grep -o '"rtsp://[^"]*"' config.json | sed 's/"//g'
|
|
|
+ fi
|
|
|
+ else
|
|
|
+ print_error "配置文件 config.json 不存在"
|
|
|
+ exit 1
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+# 测试网络连通性
|
|
|
+test_network_connectivity() {
|
|
|
+ local url=$1
|
|
|
+ print_info "测试RTSP URL: $url"
|
|
|
+
|
|
|
+ # 解析URL获取主机和端口
|
|
|
+ local host=$(echo $url | sed -n 's|rtsp://\([^:]*\):\([0-9]*\)/.*|\1|p')
|
|
|
+ local port=$(echo $url | sed -n 's|rtsp://\([^:]*\):\([0-9]*\)/.*|\2|p')
|
|
|
+
|
|
|
+ if [[ -z "$host" || -z "$port" ]]; then
|
|
|
+ print_error "无法解析RTSP URL: $url"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+
|
|
|
+ print_info "主机: $host, 端口: $port"
|
|
|
+
|
|
|
+ # DNS解析测试
|
|
|
+ print_info "测试DNS解析..."
|
|
|
+ if nslookup $host > /dev/null 2>&1; then
|
|
|
+ print_info "✅ DNS解析成功"
|
|
|
+ local ip=$(nslookup $host | grep "Address:" | tail -1 | awk '{print $2}')
|
|
|
+ print_info "解析IP: $ip"
|
|
|
+ else
|
|
|
+ print_error "❌ DNS解析失败"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 端口连通性测试
|
|
|
+ print_info "测试端口连通性..."
|
|
|
+ if timeout 10 nc -z $host $port 2>/dev/null; then
|
|
|
+ print_info "✅ 端口 $port 可达"
|
|
|
+ else
|
|
|
+ print_error "❌ 端口 $port 不可达"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+
|
|
|
+ # RTSP协议测试
|
|
|
+ print_info "测试RTSP协议响应..."
|
|
|
+ if timeout 10 nc -z $host $port 2>/dev/null; then
|
|
|
+ # 发送RTSP OPTIONS请求
|
|
|
+ local rtsp_response=$(timeout 5 bash -c "echo -e 'OPTIONS $url RTSP/1.0\r\nCSeq: 1\r\nUser-Agent: Network-Test\r\n\r\n' | nc $host $port 2>/dev/null" || echo "")
|
|
|
+
|
|
|
+ if [[ "$rtsp_response" == *"RTSP/1.0"* ]]; then
|
|
|
+ print_info "✅ RTSP服务器响应正常"
|
|
|
+ return 0
|
|
|
+ else
|
|
|
+ print_warn "⚠️ 端口可达但RTSP服务可能异常"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+ else
|
|
|
+ print_error "❌ 无法连接到RTSP服务器"
|
|
|
+ return 1
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+# 主函数
|
|
|
+main() {
|
|
|
+ echo ""
|
|
|
+ print_info "=========================================="
|
|
|
+ print_info "RTSP网络连接诊断工具"
|
|
|
+ print_info "=========================================="
|
|
|
+ echo ""
|
|
|
+
|
|
|
+ # 检查必要工具
|
|
|
+ for tool in nc nslookup timeout; do
|
|
|
+ if ! command -v $tool &> /dev/null; then
|
|
|
+ print_error "缺少必要工具: $tool"
|
|
|
+ print_info "请安装: apt-get install netcat-openbsd dnsutils coreutils"
|
|
|
+ exit 1
|
|
|
+ fi
|
|
|
+ done
|
|
|
+
|
|
|
+ # 显示网络环境信息
|
|
|
+ print_info "网络环境信息:"
|
|
|
+ echo " 主机名: $(hostname)"
|
|
|
+ echo " 网络接口: $(ip route get 8.8.8.8 | awk '{print $5; exit}')"
|
|
|
+ echo " 外网IP检测: $(timeout 5 curl -s ifconfig.me || echo "无法获取")"
|
|
|
+ echo " DNS配置:"
|
|
|
+ cat /etc/resolv.conf | grep nameserver | head -3 | sed 's/^/ /'
|
|
|
+ echo ""
|
|
|
+
|
|
|
+ # 获取RTSP URLs
|
|
|
+ print_info "从配置文件读取RTSP流地址..."
|
|
|
+ local urls=($(get_rtsp_urls))
|
|
|
+
|
|
|
+ if [[ ${#urls[@]} -eq 0 ]]; then
|
|
|
+ print_error "配置文件中未找到RTSP URL"
|
|
|
+ exit 1
|
|
|
+ fi
|
|
|
+
|
|
|
+ print_info "找到 ${#urls[@]} 个RTSP流地址"
|
|
|
+ echo ""
|
|
|
+
|
|
|
+ # 测试每个URL
|
|
|
+ local success_count=0
|
|
|
+ local total_count=${#urls[@]}
|
|
|
+
|
|
|
+ for url in "${urls[@]}"; do
|
|
|
+ print_info "========== 测试流 $((success_count + 1))/$total_count =========="
|
|
|
+ if test_network_connectivity "$url"; then
|
|
|
+ ((success_count++))
|
|
|
+ print_info "✅ 测试通过"
|
|
|
+ else
|
|
|
+ print_error "❌ 测试失败"
|
|
|
+ fi
|
|
|
+ echo ""
|
|
|
+ done
|
|
|
+
|
|
|
+ # 显示总结
|
|
|
+ print_info "=========================================="
|
|
|
+ print_info "测试完成"
|
|
|
+ print_info "=========================================="
|
|
|
+ print_info "成功: $success_count/$total_count"
|
|
|
+
|
|
|
+ if [[ $success_count -eq $total_count ]]; then
|
|
|
+ print_info "🎉 所有RTSP流连接测试通过!"
|
|
|
+ print_info "如果Docker中仍无法连接,请检查:"
|
|
|
+ print_info " 1. Docker网络模式是否为host"
|
|
|
+ print_info " 2. 容器内DNS配置是否正确"
|
|
|
+ print_info " 3. 防火墙设置是否阻止连接"
|
|
|
+ else
|
|
|
+ print_warn "⚠️ 部分RTSP流连接失败"
|
|
|
+ print_info "建议检查:"
|
|
|
+ print_info " 1. 网络连接状态"
|
|
|
+ print_info " 2. RTSP服务器状态"
|
|
|
+ print_info " 3. 防火墙配置"
|
|
|
+ fi
|
|
|
+
|
|
|
+ echo ""
|
|
|
+}
|
|
|
+
|
|
|
+# 执行主函数
|
|
|
+main
|