0

Shell脚本curl批量检测重定向次数和最终URL:3个实战脚本让网站巡检效率翻倍

2026.06.14 | youres | 6次围观

前言:为什么需要批量检测重定向次数和最终URL

网站运维中,重定向问题是最常见的隐形故障之一。一条看似正常的链接,背后可能经过了3次、5次甚至更多的跳转,每次跳转都会增加延迟、丢失查询参数,甚至导致流量追踪数据完全失效。

手动逐个检查?几十个URL就得耗费半天。用Shell脚本配合curl的-w参数,可以一次性批量获取每个URL的重定向次数最终落地地址,效率提升至少10倍。

本文分享3个不同场景的实战脚本,从基础版到进阶版,覆盖日常巡检的绝大多数需求。

核心原理:curl -w 的两个关键变量

批量检测的核心在于curl的-w(write-out)参数,它支持两个专门用于重定向统计的变量:

  • %{num_redirects} — 请求经历的重定向次数,0表示没有重定向
  • %{url_effective} — 跟随所有重定向后最终到达的URL

配合-L参数(跟随重定向)和-o /dev/null(丢弃响应体),就能高效获取每个URL的重定向信息:

curl -s -o /dev/null -w "%{num_redirects}|%{url_effective}|%{http_code}" -L "https://example.com"

输出格式用|分隔,方便后续用cutawk解析。

脚本一:基础版 — 从文件读取URL逐个检测

最常用的场景:手里有一份URL列表,想快速知道每个URL跳了几次、最终到了哪里。

使用方法

# 1. 准备URL列表文件
cat > urls.txt <<EOF
https://example.com/old-page
https://example.com/short-link
https://example.com/redirect-test
EOF

# 2. 执行检测脚本
bash check_redirects.sh urls.txt

脚本代码

#!/bin/bash
# Shell脚本curl批量检测重定向次数和最终URL - 基础版
# 用法: ./check_redirects.sh urls.txt

URL_FILE=$1
TIMEOUT=10
OUTPUT="redirect_report_$(date +%Y%m%d_%H%M%S).csv"

if [ -z "$URL_FILE" ]; then
    echo "用法: $0 <url列表文件>"
    exit 1
fi

if [ ! -f "$URL_FILE" ]; then
    echo "错误: 文件 $URL_FILE 不存在"
    exit 1
fi

# 输出CSV表头
echo "序号,原始URL,重定向次数,最终URL,HTTP状态码,响应时间(秒)" > "$OUTPUT"

count=0
while IFS= read -r url || [ -n "$url" ]; do
    # 跳过空行和注释
    [[ -z "$url" || "$url" =~ ^# ]] && continue
    
    count=$((count + 1))
    
    # 核心检测命令:获取重定向次数、最终URL、状态码、响应时间
    result=$(curl -s -o /dev/null \
        -w "%{num_redirects}|%{url_effective}|%{http_code}|%{time_total}" \
        -L \
        --connect-timeout "$TIMEOUT" \
        --max-time 30 \
        "$url" 2>&1)
    
    redirects=$(echo "$result" | cut -d'|' -f1)
    final_url=$(echo "$result" | cut -d'|' -f2)
    http_code=$(echo "$result" | cut -d'|' -f3)
    time_total=$(echo "$result" | cut -d'|' -f4)
    
    echo "$count,$url,$redirects,$final_url,$http_code,$time_total" >> "$OUTPUT"
    
    # 实时输出进度
    echo "[$count] $url -> 跳转${redirects}次 -> $final_url"
    
done < "$URL_FILE"

echo ""
echo "检测完成!共检测 $count 个URL"
echo "报告已保存到: $OUTPUT"

输出示例

序号,原始URL,重定向次数,最终URL,HTTP状态码,响应时间(秒)
1,https://example.com/old-page,2,https://example.com/new-page,200,0.45
2,https://example.com/short-link,1,https://example.com/destination,200,0.32
3,https://example.com/redirect-test,0,https://example.com/redirect-test,404,0.12

脚本二:进阶版 — xargs并行加速批量检测

当URL列表有几百上千条时,逐个串行检测太慢。用xargs的-P参数实现并行,速度提升5-10倍。

脚本代码

#!/bin/bash
# Shell脚本curl批量检测重定向次数和最终URL - xargs并行版
# 用法: ./check_redirects_parallel.sh urls.txt [并发数]

URL_FILE=$1
PARALLEL=${2:-10}
OUTPUT="redirect_parallel_$(date +%Y%m%d_%H%M%S).csv"

if [ -z "$URL_FILE" ] || [ ! -f "$URL_FILE" ]; then
    echo "用法: $0 <url列表文件> [并发数]"
    exit 1
fi

TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT

echo "序号,原始URL,重定向次数,最终URL,HTTP状态码" > "$OUTPUT"

total=$(grep -cv '^$\|^#' "$URL_FILE")
echo "开始并行检测 $total 个URL(并发数: $PARALLEL)..."

count=0
cat "$URL_FILE" | grep -v '^$\|^#' | xargs -I{} -P "$PARALLEL" bash -c '
    result=$(curl -s -o /dev/null \
        -w "%{num_redirects}|%{url_effective}|%{http_code}" \
        -L --connect-timeout 10 --max-time 30 "$1" 2>&1)
    echo "$1|$result" > "'"$TMP_DIR"'/result_$$_$RANDOM.txt"
' _ {}

idx=0
for f in "$TMP_DIR"/result_*.txt; do
    [ -f "$f" ] || continue
    idx=$((idx + 1))
    IFS='|' read -r url redirects final_url http_code < "$f"
    echo "$idx,$url,$redirects,$final_url,$http_code" >> "$OUTPUT"
done

echo "检测完成!共 $idx 个URL"
echo "报告: $OUTPUT"

关键优化点:

  • 每个并行进程输出到独立临时文件,避免并发写冲突
  • trap确保脚本退出时清理临时目录
  • 并发数可调,默认10,服务器性能好可以设到20-50

脚本三:专业版 — 带阈值告警的重定向巡检脚本

运维场景下,不仅要检测,还要能自动发现问题并告警。当重定向次数超过阈值时,脚本会标记为异常并汇总输出。

脚本代码

#!/bin/bash
# Shell脚本curl批量检测重定向次数和最终URL - 告警版
# 用法: ./check_redirects_alert.sh urls.txt [最大跳转次数阈值]

URL_FILE=$1
MAX_REDIRECTS=${2:-3}
OUTPUT="redirect_alert_$(date +%Y%m%d_%H%M%S).log"
ALERT_FILE="redirect_alert_$(date +%Y%m%d_%H%M%S)_abnormal.log"

if [ -z "$URL_FILE" ] || [ ! -f "$URL_FILE" ]; then
    echo "用法: $0 <url列表文件> [最大跳转次数阈值]"
    exit 1
fi

echo "========================================" > "$OUTPUT"
echo "重定向巡检报告 - $(date)" >> "$OUTPUT"
echo "阈值设置: 重定向次数 > $MAX_REDIRECTS 视为异常" >> "$OUTPUT"
echo "========================================" >> "$OUTPUT"
echo "" >> "$OUTPUT"

echo "原始URL,重定向次数,最终URL,HTTP状态码,是否异常" > "$ALERT_FILE"

total=0
abnormal=0
normal=0

while IFS= read -r url || [ -n "$url" ]; do
    [[ -z "$url" || "$url" =~ ^# ]] && continue
    total=$((total + 1))
    
    result=$(curl -s -o /dev/null \
        -w "%{num_redirects}|%{url_effective}|%{http_code}" \
        -L --connect-timeout 10 --max-time 30 "$url" 2>&1)
    
    redirects=$(echo "$result" | cut -d'|' -f1)
    final_url=$(echo "$result" | cut -d'|' -f2)
    http_code=$(echo "$result" | cut -d'|' -f3)
    
    if [ "$redirects" -gt "$MAX_REDIRECTS" ]; then
        status="[异常]"
        abnormal=$((abnormal + 1))
        echo "$url,$redirects,$final_url,$http_code,异常" >> "$ALERT_FILE"
    else
        status="[正常]"
        normal=$((normal + 1))
    fi
    
    echo "$status $url -> 跳转${redirects}次 -> $final_url (状态码: $http_code)" >> "$OUTPUT"
    
done < "$URL_FILE"

echo "" >> "$OUTPUT"
echo "========================================" >> "$OUTPUT"
echo "巡检汇总" >> "$OUTPUT"
echo "  总检测数: $total" >> "$OUTPUT"
echo "  正常: $normal" >> "$OUTPUT"
echo "  异常: $abnormal" >> "$OUTPUT"
echo "========================================" >> "$OUTPUT"

echo ""
echo "巡检完成!"
echo "  总计: $total 个URL"
echo "  正常: $normal | 异常: $abnormal"
echo "  详细报告: $OUTPUT"
echo "  异常列表: $ALERT_FILE"

[ "$abnormal" -gt 0 ] && exit 1
exit 0

配合crontab定时巡检:

# 每天上午9点执行巡检
0 9 * * * /path/to/check_redirects_alert.sh /path/to/urls.txt 3 >> /var/log/redirect_check.log 2>&1

三个脚本的对比与选择

  • 基础版:适合快速检查几十个URL,输出CSV格式方便导入Excel分析
  • 并行版:适合几百上千个URL的大规模巡检,速度最快
  • 告警版:适合运维场景的定时巡检,自动标记异常并生成报告

curl批量检测的注意事项

  • 超时设置:务必设置--connect-timeout--max-time,避免某个URL无响应导致整个脚本卡住
  • -L参数不可少:不加-L,curl不会跟随重定向,url_effective就是原始URL本身
  • HTTPS证书问题:内网环境如果证书不受信任,可加-k跳过验证
  • 速率控制:并行版并发数不宜过高,避免对目标服务器造成过大压力,建议10-20

相关文章推荐

版权声明

本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论