bash_agant_install/script/generate_bash_product_id.sh

53 lines
1.3 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
UUID_FILE="/etc/bash_product/BASH_PRODUCT_ID"
REGISTER_SERVER_URL="https://bash-backend.zustmyy.top/endpoint/register"
# 如果 UUID 文件不存在,就生成
if [ ! -f "$UUID_FILE" ]; then
UUID=$(cat /proc/sys/kernel/random/uuid)
echo "$UUID" > "$UUID_FILE"
chmod 644 "$UUID_FILE"
# 收集硬件信息
CPU_MODEL=$(lscpu | grep "Model name" | awk -F: '{print $2}' | sed 's/^ *//')
ARCHITECTURE=$(uname -m)
PCIE_DEVICES_RAW=$(timeout 3s lspci -nn || true)
if [ -z "$CPU_MODEL" ] || [ -z "$ARCHITECTURE" ]; then
echo "❌ 无法收集到CPU或系统架构信息注册终止"
exit 1
fi
if [ -z "$PCIE_DEVICES_RAW" ]; then
PCIE_DEVICES_JSON="[]"
else
PCIE_DEVICES_JSON="["
while IFS= read -r line; do
ESCAPED_LINE=$(echo "$line" | sed 's/"/\\"/g')
PCIE_DEVICES_JSON+="\"$ESCAPED_LINE\","
done <<< "$PCIE_DEVICES_RAW"
PCIE_DEVICES_JSON="${PCIE_DEVICES_JSON%,}]"
fi
# 发送注册
JSON_PAYLOAD=$(cat <<EOF
{
"BASH_UUID": "$UUID",
"cpu_model": "$(echo "$CPU_MODEL" | sed 's/"/\\"/g')",
"architecture": "$ARCHITECTURE",
"pcie_devices": $PCIE_DEVICES_JSON
}
EOF
)
curl -s -o /dev/null -w "%{http_code}" \
--connect-timeout 5 --max-time 10 \
-X POST "$REGISTER_SERVER_URL" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD"
fi