kunlang_config_server/mdns/install_mdns.sh
2025-12-12 09:02:06 +08:00

172 lines
4.1 KiB
Bash
Executable File
Raw 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
# --------------------------------------------
# 参数解析:必须指定 config.json 路径
# --------------------------------------------
CONFIG_PATH=""
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--config)
CONFIG_PATH="$2"
shift 2
;;
*)
echo "❌ 未知参数: $1"
exit 1
;;
esac
done
if [[ -z "$CONFIG_PATH" ]]; then
echo "❌ 必须使用 -c <config.json 路径>"
echo "示例: sudo ./install_mdns.sh -c /home/aiec/demo/config.json"
exit 1
fi
if [[ ! -f "$CONFIG_PATH" ]]; then
echo "❌ 找不到配置文件:"
echo " $CONFIG_PATH"
exit 1
fi
echo "📌 使用配置文件: $CONFIG_PATH"
# --------------------------------------------
# 1. 安装 avahi 组件
# --------------------------------------------
echo "=== 安装 Avahi 相关 deb 包 ==="
sudo dpkg -i avahi-daemon_*.deb avahi-utils_*.deb avahi-autoipd_*.deb libnss-mdns_*.deb || true
sudo apt --fix-broken install -y
echo "=== 启动 & 启用 avahi-daemon ==="
sudo systemctl enable avahi-daemon || true
sudo systemctl restart avahi-daemon
# --------------------------------------------
# 2. 安装 update_mdns.sh核心逻辑
# --------------------------------------------
echo "=== 写入 /usr/local/bin/update_mdns.sh ==="
sudo mkdir -p /usr/local/bin
sudo tee /usr/local/bin/update_mdns.sh >/dev/null <<'EOF'
#!/bin/bash
CONFIG="@CONFIG_PATH@"
TRIGGER="/run/update_mdns_request"
# --------------- 读取 device_no ---------------
DEVICE_NO=$(grep -oP '"device_no"\s*:\s*"\K[^"]+' "$CONFIG")
# 若触发文件存在,则优先使用触发文件内容
if [[ -f "$TRIGGER" ]]; then
FILE_VALUE=$(tr -d '\n' < "$TRIGGER")
if [[ -n "$FILE_VALUE" ]]; then
DEVICE_NO="$FILE_VALUE"
fi
fi
if [[ -z "$DEVICE_NO" ]]; then
echo "[update_mdns] 未找到有效 device_no"
exit 1
fi
echo "[update_mdns] 更新 hostname = $DEVICE_NO"
# --------------- 设置 hostname ---------------
hostnamectl set-hostname "$DEVICE_NO"
# 修复 /etc/hosts
if grep -q "^127.0.1.1" /etc/hosts; then
sed -i "s/^127.0.1.1.*/127.0.1.1 $DEVICE_NO/" /etc/hosts
else
echo "127.0.1.1 $DEVICE_NO" >> /etc/hosts
fi
# --------------- 更新 Avahi 服务 ---------------
mkdir -p /etc/avahi/services
tee /etc/avahi/services/webconfig.service >/dev/null <<EOF2
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h Config Page</name>
<service>
<type>_http._tcp</type>
<port>18080</port>
</service>
</service-group>
EOF2
systemctl restart avahi-daemon
# 删除触发文件,避免重复触发
rm -f "$TRIGGER"
EOF
sudo chmod +x /usr/local/bin/update_mdns.sh
# 用真实 CONFIG_PATH 替换脚本中的占位符
sudo sed -i "s|@CONFIG_PATH@|$CONFIG_PATH|g" /usr/local/bin/update_mdns.sh
# --------------------------------------------
# 3. 安装 systemd service
# --------------------------------------------
echo "=== 创建 update-mdns.service ==="
sudo tee /etc/systemd/system/update-mdns.service >/dev/null <<EOF
[Unit]
Description=Update hostname & mDNS when device_no changed
[Service]
Type=oneshot
ExecStart=/usr/local/bin/update_mdns.sh
EOF
# --------------------------------------------
# 4. 安装 systemd path watcher
# --------------------------------------------
echo "=== 创建 update-mdns.path ==="
sudo tee /etc/systemd/system/update-mdns.path >/dev/null <<EOF
[Unit]
Description=Watch for mDNS update request
[Path]
PathExists=/run/update_mdns_request
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable update-mdns.path
sudo systemctl start update-mdns.path
echo ""
echo "============================================="
echo "mDNS 自动更新机制安装完成!🎉"
echo "配置文件路径:$CONFIG_PATH"
echo ""
echo "当 C++ 程序写入: /run/update_mdns_request"
echo "内容 = 新设备号,例如: KL777"
echo "系统会立即更新:"
echo " • hostname"
echo " • /etc/hosts"
echo " • avahi 广播名字xxx.local"
echo "============================================="