Files
tools-linux/windiscover/uninstall.sh
T

44 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# 卸载 Windows/NetBIOS 主机名自动发现
set -euo pipefail
if [[ ${EUID:-$(id -u)} -ne 0 ]]; then
echo "请使用 sudo 运行: sudo bash \"$0\"" >&2
exit 1
fi
systemctl disable --now windows-name-discovery.timer 2>/dev/null || true
systemctl stop windows-name-discovery.service 2>/dev/null || true
if [[ -x /usr/local/sbin/windiscover ]]; then
if ! /usr/local/sbin/windiscover --remove; then
echo "错误: 未能删除 /etc/hosts 中的托管区块,已停止卸载。" >&2
echo "请检查 /etc/hosts 中的 windows-name-discovery 标记后重试。" >&2
exit 1
fi
else
echo "警告: 未找到 /usr/local/sbin/windiscover,无法自动清理 /etc/hosts。" >&2
echo "请手动检查并删除其中的 windows-name-discovery 托管区块。" >&2
fi
if [[ -f /etc/hosts ]] && grep -q '^# BEGIN windows-name-discovery' /etc/hosts; then
echo "错误: /etc/hosts 中仍有 windows-name-discovery 托管区块,已停止卸载。" >&2
echo "请先手动删除该区块,或修复标记后重新运行卸载脚本。" >&2
exit 1
fi
rm -f /usr/local/sbin/windiscover
rm -f /etc/systemd/system/windows-name-discovery.service
rm -f /etc/systemd/system/windows-name-discovery.timer
systemctl daemon-reload
# 只删除本工具生成的文件,保留目录中的其它内容
rm -f /var/lib/windiscover/state.json
rmdir /var/lib/windiscover 2>/dev/null || true
rm -f /usr/local/share/doc/windiscover/README.md
rmdir /usr/local/share/doc/windiscover 2>/dev/null || true
rm -f /run/lock/windiscover.lock
echo "已卸载 windiscover 程序和 systemd 单元。"
echo "/etc/hosts 的托管区块已在可用时清理;如有警告请手动检查。"