DHCP 故障排查時,需快速掃描某網段哪些 IP 未被佔用。手工 Ping 效率極低,本文提供一鍵生成「IP 跟蹤工具」批處理腳本,自動掃描指定範圍,僅輸出超時(空閒)IP 到文本文件,大幅提升運維效率。
For /L 循環遍歷 IP 尾號。ping -n 2 快速發送 2 個包,減少等待。find "Request timed out." 過濾無回應(空閒)地址。IPList.txt,最後自動用記事本打開。@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
)
cls
Echo Finished!
Notepad.exe IPList.txt
@Echo on
關鍵點:整個 For 循環命令必須在**同一行**(用 && 連接),批處理中變量用 %%f(命令行直接用 %f)。
IPTracker.bat。192.168.1.)與範圍 (1,1,100)(起始,步長,結尾)。arp -a 核對 MAC 地址表,排除防火牆阻擋 Ping 的誤判。nmap -sn 192.168.1.0/24 進行更專業的主機發現。避坑:批處理需管理員權限運行;大範圍掃描建議分段執行,避免佔用過多帶寬觸發安全設備告警。