2026年10月16日 星期五

ping class C batch script

 @echo off

setlocal EnableExtensions EnableDelayedExpansion


rem ============================================================

rem  Class C ping sweep (parallel, no popup windows)

rem  Usage: ping_scan.bat [subnet-prefix] [timeout-ms]

rem  Example: ping_scan.bat 192.168.1 300

rem ============================================================


set "subnet=%~1"

set "timeout=%~2"

if not defined timeout set "timeout=30"


if not defined subnet (

    set /p "subnet=Enter Class C subnet prefix (e.g., 192.168.1): "

)

if not defined subnet (

    echo [ERROR] Subnet cannot be empty.

    pause

    exit /b 1

)


rem Strip a trailing dot if the user typed one

if "!subnet:~-1!"=="." set "subnet=!subnet:~0,-1!"


set "work=%~dp0scan_tmp"

if exist "!work!" rd /s /q "!work!"

md "!work!"

pushd "!work!"


echo.

echo ===================================================

echo  Scanning !subnet!.1 - !subnet!.254  (timeout !timeout! ms)

echo ===================================================


rem Launch all pings in the background of THIS window (start /b = no new window)

for /L %%I in (1,1,254) do (

    start "" /b cmd /c "ping -n 1 -w !timeout! !subnet!.%%I | findstr TTL= >nul && echo.>%%I.up & echo.>%%I.done"

)


rem Wait until all 254 jobs have finished

:wait

set "done=0"

for /f %%C in ('dir /b *.done 2^>nul ^| find /c /v ""') do set "done=%%C"

if !done! LSS 254 (

    ping -n 2 127.0.0.1 >nul

    goto wait

)


echo.

echo Alive hosts:

set "count=0"

for /L %%I in (1,1,254) do (

    if exist %%I.up (

        echo   !subnet!.%%I

        set /a count+=1

    )

)


popd


echo.

echo ===================================================

echo  Scan complete. !count! host(s) alive.

echo ===================================================

pause


rd /s /q "!work!"

endlocal


rem  Powered by Claude Sonnet 5.5  20261016

沒有留言:

張貼留言