在處理 Windows 環境下的 DHCP 故障時,網管人員常需確認某個位址區間內哪些 IP 尚未被佔用。傳統做法是在命令提示字元下跑 for /L %f in (1,1,100) do ping -n 2 192.168.1.%f,但這會把「在線」與「離線」的主機全部列出,畫面資訊過多,極不方便人工篩選。
其實只需寫一支批次檔,讓它自動過濾出 Request timed out 的回應,並將結果導向文字檔,即可輕鬆獲得可用 IP 清單。以下是完整步驟:
For 迴圈與後續指令必須寫在同一行,以 && 串接):@echo off
date /t > IPList.txt
time /t >> IPList.txt
echo =========== >> IPList.txt
for /L %%f in (1,1,100) do ping.exe -n 2 192.168.1.%%f | find "Request timed out." && echo 192.168.1.%%f Timed Out >> IPList.txt && echo off
cls
echo Finished
@echo on
notepad.exe IPList.txt
192.168.1. 替換為實際網段前綴。-n 2 代表每個 IP 僅 ping 兩次,可依環境調整以平衡速度與準確度。IPList.txt 會自動記錄掃描日期、時間與分隔線,便於歷史追溯。這支批次檔一次編寫、反覆使用,能大幅縮短「尋找可用 IP」的時間,是網管工具箱裡的實用小幫手。