bash_go_service/reg.sh

66 lines
1.7 KiB
Bash
Executable File
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
# 检查是否以管理员root权限运行如需要可以取消注释
if [ "$EUID" -ne 0 ]; then
echo "请以管理员权限运行此脚本sudo"
exit 1
fi
# 收集CPU和系统架构信息
CPU_MODEL=$(lscpu | grep "Model name" | awk -F: '{print $2}' | sed 's/^ *//')
ARCHITECTURE=$(uname -m)
# 收集PCIe设备信息加超时保护3秒
PCIE_DEVICES_RAW=$(timeout 3s lspci)
# 检查是否成功获取
if [ -z "$CPU_MODEL" ] || [ -z "$ARCHITECTURE" ]; then
echo "无法收集到CPU或系统架构信息"
exit 1
fi
# 处理PCIe设备列表成JSON数组格式
if [ -z "$PCIE_DEVICES_RAW" ]; then
PCIE_DEVICES_JSON="[]"
else
PCIE_DEVICES_JSON="["
while IFS= read -r line; do
# 对每一行进行JSON转义
ESCAPED_LINE=$(echo "$line" | sed 's/"/\\"/g')
PCIE_DEVICES_JSON+="\"$ESCAPED_LINE\","
done <<< "$PCIE_DEVICES_RAW"
# 去掉最后一个逗号
PCIE_DEVICES_JSON="${PCIE_DEVICES_JSON%,}]"
fi
# 检查 BASH_PRODUCT_ID 是否为空
if [ -z "$BASH_PRODUCT_ID" ]; then
echo "错误:环境变量 BASH_PRODUCT_ID 未设置或为空"
exit 1
fi
# 将整个payload组装成JSON
JSON_PAYLOAD=$(cat <<EOF
{
"BASH_UUID": "$BASH_PRODUCT_ID",
"cpu_model": "$(echo "$CPU_MODEL" | sed 's/"/\\"/g')",
"architecture": "$ARCHITECTURE",
"pcie_devices": $PCIE_DEVICES_JSON
}
EOF
)
# 发送POST请求
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" \
--connect-timeout 5 --max-time 10 \
-X POST http://localhost:3001/endpoint/register \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD")
# 检查POST是否成功
if [ "$RESPONSE" -eq 200 ] || [ "$RESPONSE" -eq 201 ]; then
echo "信息成功发送到服务器 (HTTP $RESPONSE)"
else
echo "发送失败服务器返回HTTP状态码 $RESPONSE"
fi