Batch Files - Using ping to test network connectivity

前端 未结 8 1049
名媛妹妹
名媛妹妹 2021-01-04 17:17

Using a batch file would it be possible to do something like:

ping google.com

if return success do ECHO You are connected to the internet

else return

8条回答
  •  一整个雨季
    2021-01-04 18:00

    I understand this is an old thread, but here's my contribution anyways.

    @echo off
    
    TITLE Network Connection Watchdog
    
    :: Use Windows "Task Scheduler".
    :: Set to run at "Startup" with a delay and interval of your choice.
    :: Remember to tick "Run with highest privileges".
    
    :: Last command will only be executed if all pings fail.
    
    set /a pc=0
    set /a total=0
    
    :: Your preferences
    set if="Wi-fi" &:: Find your interface name with [netsh interface show interface].
    set ip=192.168.0.1
    set /a pt=10 &:: Set amount of pings to be executed.
    
    :loop
    set /a pc+=1
    ping %ip% -n 1 -w 100 | find "TTL=" >NUL 2>&1
    for /f %%a in ('echo %errorlevel%') do set p%pc%=%%a
    
    set /a total+=p%pc%
    if not %pc%==%pt% goto loop
    
    if not %total%==%pt% (
    goto eof
    ) else (
    netsh interface set interface %if% disable
    ping localhost -n 5 >NUL 2>&1
    netsh interface set interface %if% enable
    )
    
    :eof
    exit
    

提交回复
热议问题