身為網管,排查 DHCP 故障時常需找出某網段中未被使用的 IP 位址。傳統做法在命令提示字元用 For /L %f in (1,1,100) Do Ping.exe -n 2 192.168.1.%f 掃描 192.168.1.1~100,但輸出包含所有回應與逾時資訊,人工閱讀極不便。實際上可撰寫批次檔,僅記錄逾時(未使用)的 IP,並輸出至文字檔自動開啟。
&& 串接):@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
IPTracker.bat,關閉記事本。IPList.txt 並以記事本開啟。%%f 為變數寫法(腳本內需雙百分號)。