跳到主要内容
Linux脚本库
只读脚本测试版v0.1.1POSIX shMIT更新于

软件源状态与镜像延迟检测

列出当前发行版使用的软件源,可选测试镜像站延迟,帮助判断是否需要换源

换源之前先确认两件事:现在用的是哪个源、这个源当前快不快。这个脚本读取系统实际的软件源配置并做一次延迟测试,输出结果后再决定是否换源。

只读脚本,不修改任何源配置;延迟测试需要网络,可用 --no-test 关闭。

配置参数并生成脚本

选参数 → 即时生成 → 复制调用命令。绿色组合经过构建期校验,黄色为自定义组合。

常用预设
format

文本适合直接查看,JSON 适合二次处理

test

向源地址发起一次 HEAD 请求,测量响应时间

已验证
mirror-status.sh|5.5 KB
#!/bin/sh
# ==============================================================================
# Linux脚本库 · 软件源状态与镜像延迟检测
# ------------------------------------------------------------------------------
# 脚本 ID    : mirror-status
# 版本       : 0.1.1
# 构建标识   : 8b9dfb40
# 分类       : mirror / switch
# 危险等级   : 只读(不修改系统)
# 幂等       : 是(重复执行无副作用)
# 预演       : 不支持
# 默认参数   : format=text, test=1
# 参数覆盖   : 在命令后加 --<参数名> <值>,例如 --format json;--help 查看全部
# 文档       : https://linuxmirror.cn/script/mirror-status/
# ------------------------------------------------------------------------------
# 本文件由 Linux脚本库生成器输出,请勿手工修改。
# 执行前请阅读文档,并确认已理解脚本将要做的改动。
# ==============================================================================

set -eu
umask 022

# ---- 脚本元信息 ----
LM_SCRIPT_ID='mirror-status'
LM_SCRIPT_VERSION='0.1.1'
LM_SCRIPT_BUILD='8b9dfb40'
LM_DOC_URL='https://linuxmirror.cn/script/mirror-status/'

# ---- 公共函数 ----
die() {
  _lm_code=$1
  shift
  printf '[错误] %s\n' "$*" >&2
  exit "$_lm_code"
}

log() {
  printf '[信息] %s\n' "$*"
}

warn() {
  printf '[警告] %s\n' "$*" >&2
}

has_cmd() {
  command -v "$1" >/dev/null 2>&1
}

need_cmd() {
  for _lm_c in "$@"; do
    has_cmd "$_lm_c" || die 3 "缺少依赖命令: $_lm_c"
  done
}

need_root() {
  [ "$(id -u)" -eq 0 ] || die 4 '需要 root 权限,请使用 sudo 执行'
}

need_os_release() {
  [ -r /etc/os-release ] || die 3 '/etc/os-release 不存在,无法识别系统发行版'
}

in_container() {
  [ -f /.dockerenv ] || [ -f /run/.containerenv ]
}

has_systemd() {
  [ -d /run/systemd/system ]
}

# ---- 默认参数(可用命令行覆盖)----
LM_FORMAT='text'
LM_TEST='1'

# ---- 命令行参数(覆盖上面的默认值)----
lm_usage() {
  cat <<'LM_USAGE'
用法: bash <(curl -sSL "https://linuxmirror.cn/s/mirror-status.sh") [选项]

选项:
  -format, --format       输出格式(可选: text|json)
  -test, --test           测试镜像延迟(开关)
  -h, --help                显示本帮助
  --version                 显示脚本版本
LM_USAGE
}

LM_EXTRA_ARGS=""
while [ $# -gt 0 ]; do
  case "$1" in
    -h|--help)
      lm_usage
      exit 0
      ;;
    --version)
      printf '%s\n' "$LM_SCRIPT_ID v$LM_SCRIPT_VERSION"
      exit 0
      ;;
    --)
      shift
      break
      ;;
    -format|--format)
      [ $# -ge 2 ] || die 2 'format 需要一个值'
      LM_FORMAT=$2
      shift 2
      ;;
    --format=*|-format=*)
      LM_FORMAT=${1#*=}
      shift
      ;;
    -test|--test) LM_TEST=1; shift ;;
    --no-test|-no-test) LM_TEST=0; shift ;;
    *) LM_EXTRA_ARGS="$LM_EXTRA_ARGS $1"; shift ;;
  esac
done

# 未识别的参数留给正文(例如 --dry-run);正文可用 "$@" 读取
if [ -n "$LM_EXTRA_ARGS" ]; then
  # shellcheck disable=SC2086
  set -- $LM_EXTRA_ARGS
fi

# ---- 参数兜底校验 ----
case $LM_FORMAT in
  text|json) ;;
  *) printf '[错误] %s 取值非法: %s\n' '输出格式' "$LM_FORMAT" >&2; exit 2 ;;
esac
case $LM_TEST in
  0|1) ;;
  *) die 2 '测试镜像延迟 只能是 0 或 1' ;;
esac

# ==============================================================================
# 正文
# ==============================================================================
LM_OS='unknown'
LM_URLS=''
PRETTY_NAME=unknown
if [ -r /etc/os-release ]; then
  . /etc/os-release
  LM_OS="$PRETTY_NAME"
fi

if [ -d /etc/apt/sources.list.d ] || [ -f /etc/apt/sources.list ]; then
  LM_KIND=apt
  for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
    [ -f "$f" ] || continue
    LM_URLS="$LM_URLS $(sed -n 's/^deb[[:space:]].*\(http[^[:space:]]*\).*/\1/p' "$f")"
  done
  for f in /etc/apt/sources.list.d/*.sources; do
    [ -f "$f" ] || continue
    LM_URLS="$LM_URLS $(sed -n 's/^URIs:[[:space:]]*//p' "$f")"
  done
elif [ -d /etc/yum.repos.d ]; then
  LM_KIND=dnf
  LM_URLS=$(sed -n 's/^baseurl=[[:space:]]*//p; s/^mirrorlist=[[:space:]]*//p' /etc/yum.repos.d/*.repo 2>/dev/null)
elif [ -f /etc/apk/repositories ]; then
  LM_KIND=apk
  LM_URLS=$(sed -n 's/^[[:space:]]*//p' /etc/apk/repositories)
elif [ -f /etc/pacman.d/mirrorlist ]; then
  LM_KIND=pacman
  LM_URLS=$(sed -n 's/^Server[[:space:]]*=[[:space:]]*//p' /etc/pacman.d/mirrorlist)
else
  LM_KIND=unknown
fi

# 去重并取主机名
HOSTS=''
for u in $LM_URLS; do
  h=$(printf '%s' "$u" | sed 's#^[a-z]*://##; s#/.*##')
  [ -n "$h" ] || continue
  case " $HOSTS " in *" $h "*) continue ;; esac
  HOSTS="$HOSTS $h"
done

test_one() {
  h=$2
  if ! has_cmd curl; then
    printf '  %-32s curl 缺失,跳过\n' "$h"
    return
  fi
  code=$(curl -sS -o /dev/null -m 3 -w '%{http_code} %{time_total}' "https://$h/" 2>/dev/null || true)
  [ -n "$code" ] || code='连接失败'
  printf '  %-32s %s\n' "$h" "$code"
}

if [ "$LM_FORMAT" = 'json' ]; then
  printf '{"os":"%s","kind":"%s","hosts":"%s","tested":%s}\n' "$LM_OS" "$LM_KIND" "$HOSTS" "$LM_TEST"
  exit 0
fi

log '================ 软件源状态 ================'
log "系统 : $LM_OS"
log "类型 : $LM_KIND"
log ''
log '启用的源主机:'
for h in $HOSTS; do
  printf '  - %s\n' "$h"
done
[ -n "$HOSTS" ] || warn '未在常见路径下找到软件源配置'

if [ "$LM_TEST" = '1' ] && [ -n "$HOSTS" ]; then
  log ''
  log '--- 延迟测试(HEAD,超时 3s)---'
  for h in $HOSTS; do
    test_one x "$h"
  done
fi
log ''
log '提示:换源前先确认延迟与 DNS 解析,变更类脚本会先备份原文件。'
sha256:094f0c221b4210e101ee2e62ed23b4c3b275d1361eed205fb8c5c1509e7e9b12
固定 URL 与执行方式
# 唯一支持的调用方式:固定 URL 是静态脚本,参数在命令后覆盖
bash <(curl -sSL "https://linuxmirror.cn/s/mirror-status.sh")

# 查看全部参数
bash <(curl -sSL "https://linuxmirror.cn/s/mirror-status.sh") --help
固定 URLhttps://linuxmirror.cn/s/mirror-status.sh

URL 永远不变(版本升级也继续可用);只允许 curl 调用,浏览器与 wget 会被拒绝。

兼容矩阵

共支持 8 个发行版,均为理论支持。 系统识别统一读取 /etc/os-release, 不依赖 /etc/redhat-release。

发行版最低版本最高版本实测
Debian1113未实测
Ubuntu20.0426.04未实测
CentOS79未实测
Rocky Linux810未实测
AlmaLinux810未实测
openEuler20.03—未实测
Alpine3—未实测
Arch Linux——未实测

支持架构:x86_64 · ARM64 (aarch64)

运行要求与影响范围

运行权限
普通用户即可
网络依赖
需要访问外网 / 镜像站
幂等性
幂等,重复执行无副作用
预演支持
不支持预演

常见问题

会修改软件源吗?

不会。脚本只读取 /etc/apt/sources.list*、/etc/yum.repos.d/*.repo、/etc/apk/repositories 等现有配置。

延迟测试会请求哪些地址?

只请求当前启用源里的主机名,每个发一次 HEAD 请求,超时 3 秒。

免责声明:本脚本按"原样"提供,作者与本站不对因使用脚本造成的任何直接或间接损失负责。 请在测试环境验证后再用于生产,并自行承担执行风险。脚本由服务端按参数实时渲染, 可在页面生成器里核对完整内容后,再通过固定 URL 执行。 如发现安全问题请通过 dakerteam@163.com 联系。

固定地址(版本升级不变,参数用 query):https://linuxmirror.cn/s/mirror-status.sh· sha256 端点:https://linuxmirror.cn/s/mirror-status.sha256

审计用不可变地址(内容变更即换地址,适合复现与比对):https://linuxmirror.cn/s/mirror-status/0.1.1/

输入关键词开始搜索。脚本名、发行版、分类、文档标题都会被索引。

↑↓ 选择 · Enter 打开浏览全部脚本