Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows 批次檔追蹤 DHCP IP 位址使用狀況

編輯:Windows 7 系統介紹

網管需快速掃描 DHCP 範圍內哪些 IP 未被使用。單純 ping 迴圈會輸出大量資訊,難以閱讀。撰寫批次檔僅記錄「請求逾時」的 IP 並存成文字檔,即可快速盤點可用 IP。

批次檔內容(IPTracker.bat)

@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." >nul
    If ErrorLevel 1 Echo 192.168.1.%%f Timed Out >> IPList.txt
)
cls
Echo Finished!
Notepad.exe IPList.txt

使用說明

  1. 將上述碼存為 IPTracker.bat。
  2. 修改 192.168.1 為目標網段,(1,1,100) 為掃描範圍(起始、遞增、結束)。
  3. 雙擊執行,完成後自動開啟 IPList.txt 顯示未使用 IP 清單。

原理

  • For /L %%f in (start,step,end) 產生迴圈變數。
  • Ping -n 2 僅送 2 個封包加速掃描。
  • Find "Request timed out." 偵測無回應,ErrorLevel 1 表示找到(即逾時)。
  • 結果寫入文字檔,避免螢幕閃爍大量文字。

注意

  • 僅適用 Windows XP Professional 及以上版本(Home 版無 Find 指令)。
  • 防火牆封鎖 ICMP 會導致誤判為「未使用」,掃描前建議暫時開放 ICMP 回應。
  • 生產環境建議搭配 DHCP 伺服器管理主控台(dhcpmgmt.msc)查看租約,更準確。

此批次檔適合快速臨時盤點,一寫永用,大幅減少人工比對負擔。

Copyright © Windows教程網 All Rights Reserved