添加windows主机名发现和按需挂载共享工具.
This commit is contained in:
commit
d7b8a21ca5
12 files changed
+2119
No files matched your search
@@ -0,0 +1,76 @@
|
||||
# windiscover — Windows 主机名自动发现
|
||||
|
||||
在 Linux 上自动发现局域网内 Windows 主机的 **NetBIOS 主机名**,并维护
|
||||
`/etc/hosts` 中的托管区块,使 `ping win-pc`、`smb://win-pc/share`、`ssh win-pc`
|
||||
等可以直接使用主机名,无需手工维护 hosts。
|
||||
|
||||
需要 Linux + systemd、Python 3.8+、iproute2(`ip` 命令)和 root 权限。
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
sudo bash install.sh
|
||||
```
|
||||
|
||||
安装后会立即执行一次扫描,并启用一个 systemd 定时器,之后每 5 分钟自动刷新。
|
||||
|
||||
## 日常使用
|
||||
|
||||
```bash
|
||||
windiscover # 扫描并显示发现的主机
|
||||
windiscover --no-sweep # 快速模式(广播 + ARP 邻居 + 已知主机)
|
||||
windiscover --json # JSON 输出
|
||||
sudo windiscover --update-hosts # 扫描并更新 /etc/hosts
|
||||
sudo windiscover --sweep-all --update-hosts # 全量扫描本机所有 IPv4 网段
|
||||
sudo windiscover --remove # 只删除 /etc/hosts 中的托管区块
|
||||
```
|
||||
|
||||
安装后的文件:
|
||||
|
||||
| 路径 | 用途 |
|
||||
| --- | --- |
|
||||
| `/usr/local/sbin/windiscover` | 扫描程序 |
|
||||
| `/etc/systemd/system/windows-name-discovery.service` | 单次扫描服务 |
|
||||
| `/etc/systemd/system/windows-name-discovery.timer` | 每 5 分钟触发 |
|
||||
| `/var/lib/windiscover/state.json` | 已发现主机状态(含未响应计数) |
|
||||
| `/etc/hosts` | 自动维护的托管区块 |
|
||||
|
||||
## 工作原理
|
||||
|
||||
1. 向本机各 IPv4 网段的广播地址发送 NetBIOS 节点状态查询(NBSTAT,UDP/137);
|
||||
2. 扫描默认网关所在 `/24`,并每次轮换一个本地 `/24`(约 40 分钟覆盖全部网段);
|
||||
3. 查询 ARP 邻居表和上次已发现的主机(即使它们暂时从 ARP 表消失);
|
||||
4. 解析应答中的唯一 `<00>` NetBIOS 名称、工作组和 MAC;
|
||||
5. 写入 `/etc/hosts` 的托管区块:
|
||||
|
||||
```text
|
||||
# BEGIN windows-name-discovery (managed automatically, do not edit)
|
||||
192.0.2.10 win-pc
|
||||
...
|
||||
# END windows-name-discovery
|
||||
```
|
||||
|
||||
主机偶尔一次没响应不会被删除;状态文件记录未响应次数,默认连续 6 次
|
||||
(主网段约 30 分钟)无响应才清除。手动写在托管区块之外的 hosts 条目优先,
|
||||
脚本不会覆盖同名记录。
|
||||
|
||||
## 限制与注意
|
||||
|
||||
- 仅支持 IPv4 和 NetBIOS。关闭了 NetBIOS 名称服务、仅依赖 LLMNR/mDNS 的
|
||||
Windows 主机无法通过此方式发现。
|
||||
- NetBIOS 应答没有认证,局域网内任何主机都能伪造应答。请只在可信局域网
|
||||
中使用;本工具只采集名称和地址,不会执行应答内容,但解析结果仍可能被投毒。
|
||||
- 不修改 `nsswitch.conf` 或 DNS 配置,依赖 `hosts: files ...` 中 `files` 优先
|
||||
(Ubuntu 默认即如此)。
|
||||
- 定时扫描会产生少量 UDP/137 和 ARP 流量,默认每次扫描两个 `/24`。
|
||||
- 需要 root 权限才能更新 `/etc/hosts` 和绑定 137 端口;非 root 运行时自动
|
||||
改用临时端口,只读显示结果。
|
||||
|
||||
## 卸载
|
||||
|
||||
```bash
|
||||
sudo bash uninstall.sh
|
||||
```
|
||||
|
||||
会停止并删除 systemd 单元、程序、状态文件,以及 `/etc/hosts` 中的托管区块;
|
||||
`/etc/hosts` 的其它内容不受影响。
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/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
|
||||
|
||||
if [[ ! -x /usr/bin/python3 ]]; then
|
||||
echo "错误: 需要 /usr/bin/python3" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! /usr/bin/python3 -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 8) else 1)'; then
|
||||
echo "错误: 需要 Python 3.8 或更高版本(/usr/bin/python3)" >&2
|
||||
exit 1
|
||||
fi
|
||||
for cmd in install systemctl ip readlink; do
|
||||
if ! command -v "$cmd" >/dev/null 2>&1; then
|
||||
echo "错误: 缺少依赖 $cmd" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
here="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
|
||||
|
||||
install -d -m 0755 /usr/local/sbin
|
||||
install -d -m 0755 /usr/local/share/doc/windiscover
|
||||
install -d -m 0755 /var/lib/windiscover
|
||||
install -m 0755 "$here/windiscover" /usr/local/sbin/windiscover
|
||||
install -m 0644 "$here/README.md" /usr/local/share/doc/windiscover/README.md
|
||||
install -m 0644 "$here/windows-name-discovery.service" \
|
||||
/etc/systemd/system/windows-name-discovery.service
|
||||
install -m 0644 "$here/windows-name-discovery.timer" \
|
||||
/etc/systemd/system/windows-name-discovery.timer
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now windows-name-discovery.timer
|
||||
|
||||
echo "正在执行首次扫描(约 10 秒)..."
|
||||
systemctl start windows-name-discovery.service || true
|
||||
systemctl --no-pager --full status windows-name-discovery.service || true
|
||||
|
||||
echo
|
||||
echo "===== /etc/hosts 中自动维护的记录 ====="
|
||||
awk '/^# BEGIN windows-name-discovery/{show=1; next} /^# END windows-name-discovery/{show=0} show {print}' \
|
||||
/etc/hosts || true
|
||||
|
||||
echo
|
||||
echo "===== 解析验证 ====="
|
||||
mapfile -t names < <(
|
||||
awk '/^# BEGIN windows-name-discovery/{show=1; next}
|
||||
/^# END windows-name-discovery/{show=0}
|
||||
show {print $2}' /etc/hosts
|
||||
)
|
||||
if [[ ${#names[@]} -eq 0 ]]; then
|
||||
echo "尚未发现主机(局域网内可能没有开启 NetBIOS 名称服务的 Windows 主机)。"
|
||||
else
|
||||
for name in "${names[@]:0:5}"; do
|
||||
getent hosts "$name" || echo "$name 解析失败"
|
||||
done
|
||||
if [[ ${#names[@]} -gt 5 ]]; then
|
||||
echo "... 共 ${#names[@]} 条,完整列表请运行: windiscover"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo
|
||||
systemctl list-timers windows-name-discovery.timer --no-pager || true
|
||||
echo
|
||||
echo "安装完成。之后每 5 分钟自动扫描一次。"
|
||||
echo "手动查看: windiscover"
|
||||
echo "手动全量刷新: sudo windiscover --sweep-all --update-hosts"
|
||||
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/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 的托管区块已在可用时清理;如有警告请手动检查。"
|
||||
Executable
+1085
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=Discover Windows/NetBIOS hostnames and update /etc/hosts
|
||||
Documentation=file:///usr/local/share/doc/windiscover/README.md
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/python3 -I /usr/local/sbin/windiscover --update-hosts --quiet
|
||||
Nice=10
|
||||
IOSchedulingClass=idle
|
||||
PrivateTmp=yes
|
||||
ProtectHome=yes
|
||||
ProtectSystem=true
|
||||
NoNewPrivileges=yes
|
||||
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Periodically discover Windows/NetBIOS hostnames
|
||||
|
||||
[Timer]
|
||||
OnBootSec=30s
|
||||
OnUnitActiveSec=5min
|
||||
AccuracySec=30s
|
||||
RandomizedDelaySec=15s
|
||||
Persistent=false
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
Reference in new issue
Block a user