Skip to content

Instantly share code, notes, and snippets.

@scomper
Created March 21, 2026 10:26
Show Gist options
  • Select an option

  • Save scomper/128db7b53a0d7731bd8342ba96d31ed7 to your computer and use it in GitHub Desktop.

Select an option

Save scomper/128db7b53a0d7731bd8342ba96d31ed7 to your computer and use it in GitHub Desktop.
网络诊断收集脚本 v1.0 - 自动收集并上传到 GitHub 私有仓库
#!/bin/bash
#
# 网络诊断结果收集与上传脚本 v1.0
# 用途: 运行诊断并将结果上传到 GitHub 仓库
# 作者: Walter
# 时间: 2026-03-21
#
set -e
# ═══════════════════════════════════════════════════════════════
# 配置
# ═══════════════════════════════════════════════════════════════
# GitHub 配置
GITHUB_OWNER="scomper" # GitHub 用户名
GITHUB_REPO="network-diagnostics" # 仓库名
GITHUB_TOKEN="${GITHUB_TOKEN:-}" # 从环境变量读取
DIAG_SCRIPT_URL="https://gist.githubusercontent.com/scomper/5cc18b92fef2344e842b4db0296a521d/raw/network-diagnostic.sh"
# 本地配置
HOSTNAME=$(hostname -s)
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
DATE=$(date +%Y-%m-%d)
RESULT_FILE="/tmp/netdiag-${HOSTNAME}-${TIMESTAMP}.log"
JSON_FILE="/tmp/netdiag-${HOSTNAME}-${TIMESTAMP}.json"
# ═══════════════════════════════════════════════════════════════
# 颜色输出
# ═══════════════════════════════════════════════════════════════
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_ok() { echo -e "${GREEN}✅ $1${NC}"; }
print_warn() { echo -e "${YELLOW}⚠️ $1${NC}"; }
print_error() { echo -e "${RED}❌ $1${NC}"; }
print_info() { echo -e "${BLUE}ℹ️ $1${NC}"; }
print_header() { echo -e "${CYAN}════════════════════════════════════════════════════════════════${NC}"; }
# ═══════════════════════════════════════════════════════════════
# 功能函数
# ═══════════════════════════════════════════════════════════════
# 检查 GitHub Token
check_github_token() {
if [ -z "$GITHUB_TOKEN" ]; then
print_error "未设置 GITHUB_TOKEN 环境变量"
echo ""
echo "请设置 GitHub Personal Access Token:"
echo " export GITHUB_TOKEN='ghp_xxxxxxxxxxxxxxxxxxxx'"
echo ""
echo "获取方式:"
echo " 1. 访问 https://github.com/settings/tokens"
echo " 2. 点击 'Generate new token (classic)'"
echo " 3. 勾选 'repo' 权限"
exit 1
fi
}
# 运行诊断
run_diagnosis() {
print_header
echo "🚀 开始网络诊断收集"
print_header
echo ""
print_info "下载并运行诊断脚本..."
# 下载并运行诊断
if ! curl -fsSL "$DIAG_SCRIPT_URL" -o /tmp/network-diagnostic.sh 2>/dev/null; then
print_error "下载诊断脚本失败"
exit 1
fi
bash /tmp/network-diagnostic.sh > "$RESULT_FILE" 2>&1
print_ok "诊断完成"
echo " 结果文件: $RESULT_FILE"
}
# 解析诊断结果为 JSON
parse_to_json() {
print_info "解析诊断结果..."
# 提取关键信息
local wifi_ip=$(grep -E "Wi-Fi.*IP 地址" "$RESULT_FILE" | head -1 | awk '{print $NF}')
local zerotier_ip=$(grep "feth2110:" "$RESULT_FILE" | awk '{print $2}')
local zerotier_status=$(grep "节点状态" -A1 "$RESULT_FILE" | tail -1 | xargs)
local surge_status=$(grep -E "Surge 运行" "$RESULT_FILE" | head -1)
local proxy_ip=$(grep -A1 "通过 Surge 代理" "$RESULT_FILE" | tail -1 | awk '{print $2}')
local direct_ip=$(grep -A1 "直连出口" "$RESULT_FILE" | tail -1 | awk '{print $2}')
local issues=$(grep -E "✅|❌|⚠️" "$RESULT_FILE" | wc -l | tr -d ' ')
# 构建 JSON
cat > "$JSON_FILE" << EOF
{
"timestamp": "$TIMESTAMP",
"date": "$DATE",
"hostname": "$HOSTNAME",
"network": {
"wifi_ip": "${wifi_ip:-null}",
"zerotier_ip": "${zerotier_ip:-null}",
"proxy_ip": "${proxy_ip:-null}",
"direct_ip": "${direct_ip:-null}"
},
"services": {
"zerotier": {
"running": $(echo "$zerotier_status" | grep -q "ONLINE\|TUNNELED" && echo "true" || echo "false"),
"status": "${zerotier_status:-unknown}"
},
"surge": {
"running": $(echo "$surge_status" | grep -q "运行中" && echo "true" || echo "false")
}
},
"issues_count": $issues,
"raw_log": "$(cat "$RESULT_FILE" | sed 's/"/\\"/g' | tr '\n' ' ')"
}
EOF
print_ok "JSON 解析完成"
}
# 上传到 GitHub
upload_to_github() {
print_info "上传到 GitHub..."
# 构建 API URL
local api_url="https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/contents/reports/${DATE}/${HOSTNAME}-${TIMESTAMP}.log"
# 编码文件内容
local content=$(base64 -i "$RESULT_FILE")
# 检查文件是否已存在
local sha=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"$api_url" | grep -o '"sha":"[^"]*"' | cut -d'"' -f4)
# 构建请求数据
local data
if [ -n "$sha" ]; then
# 更新现有文件
data="{\"message\":\"Update network diagnostic for $HOSTNAME\",\"content\":\"$content\",\"sha\":\"$sha\"}"
else
# 创建新文件
data="{\"message\":\"Add network diagnostic for $HOSTNAME\",\"content\":\"$content\"}"
fi
# 上传
local response=$(curl -s -X PUT \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-d "$data" \
"$api_url")
if echo "$response" | grep -q '"content"'; then
print_ok "上传成功"
local file_url=$(echo "$response" | grep -o '"html_url":"[^"]*"' | head -1 | cut -d'"' -f4)
echo " 文件链接: $file_url"
# 返回 URL 供后续使用
echo "$file_url" > /tmp/netdiag_last_upload_url.txt
else
print_error "上传失败"
echo "$response" | head -5
return 1
fi
}
# 创建或更新 Issue(汇总)
update_summary_issue() {
print_info "更新汇总 Issue..."
local issue_title="📊 网络诊断汇总 - $DATE"
local issue_body="## 诊断时间: $DATE\n\n"
issue_body+="### 最新诊断结果\n\n"
issue_body+="| 设备 | Wi-Fi IP | Zerotier IP | 代理 IP | 状态 |\n"
issue_body+="|------|----------|-------------|---------|------|\n"
issue_body+="| $HOSTNAME | ${wifi_ip:-N/A} | ${zerotier_ip:-N/A} | ${proxy_ip:-N/A} | ✅ |\n\n"
issue_body+="### 完整报告\n\n"
issue_body+="查看完整报告: https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/tree/main/reports/${DATE}\n\n"
issue_body+="---\n\n"
issue_body+"*自动更新于 $(date '+%Y-%m-%d %H:%M:%S')*"
# 查找是否已有今日的汇总 Issue
local existing_issue=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues?state=open" | \
grep -o '"number":[0-9]*.*"title":"📊 网络诊断汇总 - '$DATE'"' | grep -o '"number":[0-9]*' | cut -d: -f2)
if [ -n "$existing_issue" ]; then
# 更新现有 Issue
curl -s -X PATCH \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"body\":\"$issue_body\"}" \
"https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/$existing_issue" > /dev/null
print_ok "汇总 Issue 更新成功"
else
# 创建新 Issue
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"title\":\"$issue_title\",\"body\":\"$issue_body\",\"labels\":[\"network-diagnostic\",\"auto-generated\"]}" \
"https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues" > /dev/null
print_ok "汇总 Issue 创建成功"
fi
}
# ═══════════════════════════════════════════════════════════════
# 主程序
# ═══════════════════════════════════════════════════════════════
main() {
# 检查参数
if [ "$1" = "--local" ]; then
# 仅本地运行,不上传
run_diagnosis
echo ""
print_info "诊断结果 (本地模式):"
cat "$RESULT_FILE"
exit 0
fi
# 完整流程
check_github_token
run_diagnosis
parse_to_json
upload_to_github
update_summary_issue
# 清理
rm -f "$RESULT_FILE" "$JSON_FILE" /tmp/network-diagnostic.sh
echo ""
print_header
echo "🎉 网络诊断收集完成"
print_header
echo ""
print_info "查看结果:"
echo " https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/tree/main/reports/${DATE}"
if [ -f /tmp/netdiag_last_upload_url.txt ]; then
echo " 本次报告: $(cat /tmp/netdiag_last_upload_url.txt)"
fi
}
# 运行
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment