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

DHCP 管理技巧:用批處理腳本快速掃描並導出未使用的 IP 地址

編輯:Windows XP 系統介紹

痛點:手動 Ping 掃描太繁瑣

網管在排查 DHCP 故障時,常需要知道某個網段(如 192.168.1.1~100)哪些 IP 尚未被佔用。傳統 for /L %f in (1,1,100) do ping 192.168.1.%f 會輸出大量「回覆」與「逾時」混雜的資訊,肉眼篩選極其低效。

解決方案:只輸出「未使用」IP 的批處理腳本

建立一個 IPTracker.bat,利用 find "Request timed out." 過濾出無回應的 IP,並自動寫入帶時間戳的文本文件:

@echo off
date /t > IPList.txt
time /t >> IPList.txt
echo =========== >> IPList.txt
for /L %%f in (1,1,100) do (
    ping -n 2 192.168.1.%%f | find "Request timed out." >nul && echo 192.168.1.%%f Timed Out >> IPList.txt
)
cls
echo Finished!
notepad.exe IPList.txt

使用說明

  1. 將上述代碼存為 IPTracker.bat(注意 for 迴圈變數在批處理中需雙百分號 %%f)。
  2. 根據實際網段修改 IP 前綴(如 192.168.1.)與範圍 (1,1,100)。
  3. 雙擊執行,稍等片刻記事本自動彈出,清單僅列出「逾時未回應」的可用 IP。

進階提示

  • 此腳本僅適用於 Windows XP Professional 及以上版本。
  • 若網段較大(/24),建議增加 ping -w 200 縮短單次等待時間。
  • 也可結合 arp -a 輔助驗證 MAC 地址是否存在,進一步排除靜態綁定但離線的設備。
Copyright © Windows教程網 All Rights Reserved