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

XP 批次腳本掃描區網閒置 IP:只輸出未使用位址到文字檔,網管必備

編輯:XP 系統基礎知識

排查 DHCP 衝突、規劃靜態 IP 分配時,需要快速找出某網段 (如 192.168.1.1–100) 哪些 IP 尚未被佔用。手動 ping 太慢、輸出太雜。本文提供一支批次檔,自動只記錄「逾時未回應」的 IP 到 IPList.txt,雙擊即跑,網管神器。

批次檔完整碼 (存為 IPTracker.bat)

@echo off
date /t > IPList.txt
time /t >> IPList.txt
echo =========== >> IPList.txt
for /L %%f in (1,1,100) do (
    ping -n 1 -w 200 192.168.1.%%f | find "Request timed out." >nul
    if errorlevel 1 (
        echo 192.168.1.%%f Timed Out >> IPList.txt
    )
)
cls
echo Finished! IPList.txt 已產生於 %cd%
notepad.exe IPList.txt
@echo on

關鍵參數解析

  • for /L %%f in (1,1,100):迴圈 1 到 100,對應 IP 尾數。
  • ping -n 1 -w 200:只送 1 個封包、等待 200 ms,大幅縮短單次耗時 (預設 4 封包、4 秒)。
  • find "Request timed out.":尋找逾時字串,回傳 errorlevel 1 代表找到 (即無回應)。
  • >nul:隱藏 ping 詳細輸出,只留結果判斷。
  • >> IPList.txt:追加寫入,保留時間戳與分隔線。

自訂你的網段

將腳本中 192.168.1. 替換為你的實際網段前綴 (如 10.0.0.、172.16.5.),並調整迴圈範圍 (1,1,254) 覆蓋全 C 類網段。

進階版:並行掃描 (需 PowerShell 2.0+)

1..254 | ForEach-Object -Parallel {
    $ip = "192.168.1.$_"
    if (!(Test-Connection -Count 1 -Quiet -TimeToLive 200 $ip)) {
        "$ip Timed Out" | Out-File -Append IPList.txt
    }
} -ThrottleLimit 50

並行 50 執行緒,掃完 C 類網段約 10 秒內完成,輸出同格式 IPList.txt。

適用環境與限制

  • ✅ Windows XP Professional / Server 2003 / Vista / 7 / 10 / 11 (批次檔版)。
  • ✅ PowerShell 版需 Win7/Server 2008 R2 以上。
  • ⚠️ 目標主機啟用 Windows 防火牆「阻擋 ICMP 回應」會導致誤判為「閒置」;建議暫時開放 ICMP 或改用 ARP 掃描 (arp -a) 輔助驗證。
  • ⚠️ 僅適用 IPv4 區域網段,跨路由/VPN 需調整 TTL 與閘道設定。

存一支 IPTracker.bat 放在桌面/工具箱,下次規劃靜態 IP、排查 IP 衝突,雙擊跑完喝口咖啡,結果自動開啟記事本,效率直線上升。

Copyright © Windows教程網 All Rights Reserved