network-test.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. # RTSP网络连接诊断脚本
  3. set -e
  4. # 颜色定义
  5. RED='\033[0;31m'
  6. GREEN='\033[0;32m'
  7. YELLOW='\033[1;33m'
  8. NC='\033[0m' # No Color
  9. print_info() {
  10. echo -e "${GREEN}[INFO]${NC} $1"
  11. }
  12. print_warn() {
  13. echo -e "${YELLOW}[WARN]${NC} $1"
  14. }
  15. print_error() {
  16. echo -e "${RED}[ERROR]${NC} $1"
  17. }
  18. # 从配置文件读取RTSP URLs
  19. get_rtsp_urls() {
  20. if [[ -f "config.json" ]]; then
  21. # 使用jq解析JSON(如果可用)
  22. if command -v jq &> /dev/null; then
  23. jq -r '.streams[].rtsp_url' config.json
  24. else
  25. # 简单的grep解析
  26. grep -o '"rtsp://[^"]*"' config.json | sed 's/"//g'
  27. fi
  28. else
  29. print_error "配置文件 config.json 不存在"
  30. exit 1
  31. fi
  32. }
  33. # 测试网络连通性
  34. test_network_connectivity() {
  35. local url=$1
  36. print_info "测试RTSP URL: $url"
  37. # 解析URL获取主机和端口
  38. local host=$(echo $url | sed -n 's|rtsp://\([^:]*\):\([0-9]*\)/.*|\1|p')
  39. local port=$(echo $url | sed -n 's|rtsp://\([^:]*\):\([0-9]*\)/.*|\2|p')
  40. if [[ -z "$host" || -z "$port" ]]; then
  41. print_error "无法解析RTSP URL: $url"
  42. return 1
  43. fi
  44. print_info "主机: $host, 端口: $port"
  45. # DNS解析测试(改进的检测方法)
  46. print_info "测试DNS解析..."
  47. local dns_result=$(nslookup $host 2>/dev/null | grep -A 1 "Name:" | grep "Address:" | awk '{print $2}' | head -1)
  48. if [[ -z "$dns_result" ]]; then
  49. # 尝试使用dig命令
  50. dns_result=$(dig +short $host 2>/dev/null | head -1)
  51. fi
  52. if [[ -z "$dns_result" ]]; then
  53. # 尝试使用host命令
  54. dns_result=$(host $host 2>/dev/null | awk '{print $NF}' | head -1)
  55. fi
  56. if [[ -n "$dns_result" && "$dns_result" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  57. print_info "✅ DNS解析成功"
  58. print_info "解析IP: $dns_result"
  59. else
  60. print_error "❌ DNS解析失败"
  61. print_info "调试信息: nslookup输出:"
  62. nslookup $host 2>&1 | sed 's/^/ /'
  63. return 1
  64. fi
  65. # 端口连通性测试(增强版)
  66. print_info "测试端口连通性..."
  67. # 尝试多种连接方法
  68. local connection_success=false
  69. # 方法1: 使用nc (netcat)
  70. if command -v nc >/dev/null 2>&1; then
  71. if timeout 10 nc -z $host $port 2>/dev/null; then
  72. print_info "✅ 端口 $port 可达 (nc测试)"
  73. connection_success=true
  74. fi
  75. fi
  76. # 方法2: 使用telnet
  77. if [[ "$connection_success" == "false" ]] && command -v telnet >/dev/null 2>&1; then
  78. if timeout 10 bash -c "echo '' | telnet $host $port" 2>/dev/null | grep -q "Connected"; then
  79. print_info "✅ 端口 $port 可达 (telnet测试)"
  80. connection_success=true
  81. fi
  82. fi
  83. # 方法3: 使用/dev/tcp (bash内置)
  84. if [[ "$connection_success" == "false" ]]; then
  85. if timeout 10 bash -c "exec 3<>/dev/tcp/$host/$port && exec 3<&-" 2>/dev/null; then
  86. print_info "✅ 端口 $port 可达 (bash TCP测试)"
  87. connection_success=true
  88. fi
  89. fi
  90. if [[ "$connection_success" == "false" ]]; then
  91. print_error "❌ 端口 $port 不可达"
  92. print_info "详细诊断:"
  93. print_info " - 尝试ping主机: $(ping -c 1 -W 5 $host >/dev/null 2>&1 && echo '成功' || echo '失败')"
  94. print_info " - 尝试traceroute: $(command -v traceroute >/dev/null && echo '可用' || echo '不可用')"
  95. if command -v traceroute >/dev/null 2>&1; then
  96. print_info "路由跟踪 (前3跳):"
  97. timeout 10 traceroute -m 3 $host 2>/dev/null | head -4 | sed 's/^/ /' || echo " traceroute失败"
  98. fi
  99. return 1
  100. fi
  101. # RTSP协议测试(简化版)
  102. print_info "测试RTSP协议响应..."
  103. if command -v nc >/dev/null 2>&1; then
  104. 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 "")
  105. if [[ "$rtsp_response" == *"RTSP/1.0"* ]]; then
  106. print_info "✅ RTSP服务器响应正常"
  107. print_info "服务器响应: $(echo "$rtsp_response" | head -1 | tr -d '\r\n')"
  108. return 0
  109. else
  110. print_warn "⚠️ 端口可达但RTSP服务可能异常"
  111. print_info "尝试的RTSP请求无响应,可能是:"
  112. print_info " 1. RTSP服务器需要认证"
  113. print_info " 2. 服务器配置了特殊的连接要求"
  114. print_info " 3. 端口上运行的不是RTSP服务"
  115. return 0 # 端口可达就认为基本连接正常
  116. fi
  117. else
  118. print_info "✅ 基本连接测试通过(无法进行RTSP协议测试)"
  119. return 0
  120. fi
  121. }
  122. # 主函数
  123. main() {
  124. echo ""
  125. print_info "=========================================="
  126. print_info "RTSP网络连接诊断工具"
  127. print_info "=========================================="
  128. echo ""
  129. # 检查必要工具
  130. for tool in nc nslookup timeout; do
  131. if ! command -v $tool &> /dev/null; then
  132. print_error "缺少必要工具: $tool"
  133. print_info "请安装: apt-get install netcat-openbsd dnsutils coreutils"
  134. exit 1
  135. fi
  136. done
  137. # 显示网络环境信息
  138. print_info "网络环境信息:"
  139. echo " 主机名: $(hostname)"
  140. echo " 网络接口: $(ip route get 8.8.8.8 | awk '{print $5; exit}')"
  141. echo " 外网IP检测: $(timeout 5 curl -s ifconfig.me || echo "无法获取")"
  142. echo " DNS配置:"
  143. cat /etc/resolv.conf | grep nameserver | head -3 | sed 's/^/ /'
  144. echo ""
  145. # 获取RTSP URLs
  146. print_info "从配置文件读取RTSP流地址..."
  147. local urls=($(get_rtsp_urls))
  148. if [[ ${#urls[@]} -eq 0 ]]; then
  149. print_error "配置文件中未找到RTSP URL"
  150. exit 1
  151. fi
  152. print_info "找到 ${#urls[@]} 个RTSP流地址"
  153. echo ""
  154. # 测试每个URL
  155. local success_count=0
  156. local total_count=${#urls[@]}
  157. for url in "${urls[@]}"; do
  158. print_info "========== 测试流 $((success_count + 1))/$total_count =========="
  159. if test_network_connectivity "$url"; then
  160. ((success_count++))
  161. print_info "✅ 测试通过"
  162. else
  163. print_error "❌ 测试失败"
  164. fi
  165. echo ""
  166. done
  167. # 显示总结
  168. print_info "=========================================="
  169. print_info "测试完成"
  170. print_info "=========================================="
  171. print_info "成功: $success_count/$total_count"
  172. if [[ $success_count -eq $total_count ]]; then
  173. print_info "🎉 所有RTSP流连接测试通过!"
  174. print_info "如果Docker中仍无法连接,请检查:"
  175. print_info " 1. Docker网络模式是否为host"
  176. print_info " 2. 容器内DNS配置是否正确"
  177. print_info " 3. 防火墙设置是否阻止连接"
  178. else
  179. print_warn "⚠️ 部分RTSP流连接失败"
  180. print_info "建议检查:"
  181. print_info " 1. 网络连接状态"
  182. print_info " 2. RTSP服务器状态"
  183. print_info " 3. 防火墙配置"
  184. fi
  185. echo ""
  186. }
  187. # 执行主函数
  188. main