網管需在 DHCP 範圍 (如 192.168.1.1~100) 快速找出**未被使用的 IP** 以便靜態指派。XP 內建 `ping` + `for` 迴圈 + `find` 過濾「逾時」回應,批次檔一鍵掃描並輸出純淨可用 IP 清單。
IPTracker.bat)@echo off
echo 正在掃描 192.168.1.1-100 未使用 IP,請稍候...
date /t > IPList.txt
time /t >> IPList.txt
echo =========== >> IPList.txt
for /L %%f in (1,1,100) do (
ping -n 2 -w 500 192.168.1.%%f | find "Request timed out." >nul
if errorlevel 1 (
echo 192.168.1.%%f [在線] >> IPList.txt
) else (
echo 192.168.1.%%f [離線/可用] >> IPList.txt
)
)
cls
echo 掃描完成,結果已儲存至 IPList.txt
notepad IPList.txt
| 參數 | 說明 |
|---|---|
for /L %%f in (1,1,100) | 迴圈變數 %%f 從 1 遞增 1 至 100 (對應 IP 最後一段) |
ping -n 2 | 發送 2 個 ICMP 封包 (預設 4 個,2 個足夠判斷) |
-w 500 | 等待逾時 500 毫秒 (預設 4000ms,加快掃描速度) |
find "Request timed out." | 尋找逾時字串 (英文版系統);中文系統請改 find "請求逾時。" |
errorlevel 1 | `find` 找到字串 → errorlevel 0;**找不到** (有回應) → errorlevel 1 |
IPList.txt)2024/05/15 14:30:22 =========== 192.168.1.1 [在線] (閘道) 192.168.1.2 [離線/可用] 192.168.1.3 [在線] (印表機) 192.168.1.4 [離線/可用] ... 192.168.1.100 [在線] (NAS)
HKEY_CURRENT_USER\Control Panel\International\LocaleName` 動態切換 `find` 字串。start /b ping ... 同時發送多個 ping,大幅縮短掃描時間 (需處理輸出同步)。