batch-filevariablesconnectionping

Checking Internet connectivity with a Batch file


So recently my internet connection have been really unsatisfactory so I'm trying to gather as much data as I can about when and for how much time the outages last. I tried some "connectivity monitoring" programs but they don't work as I want them to, so I decided to make one.

I am a complete noob in batch but from googling stuff for the past hour I came up with this:

SET status="

:start_test
timeout 5
ping -n 2 -w 700 www.google.com | find "bytes="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.

) ELSE (
    SET internet=Not connected to the internet.
)
IF %status%==%internet%(
goto :start_test
) ELSE (
goto :teller 
)

:teller
echo ------------------------------------------
echo %internet%
@echo %time%
echo ------------------------------------------
SET status=%internet%
goto :start_test

tl;dr- it checks the internet connection by pining google.com and writes a massage every time the internet connection status changes

but the problem is that it doesn't work and I don't know why, when I try to run it the console opens, goes through the first few lines and closes itself.

Help

EDIT: it works now, this is how the file looks now (just like what Mofi said):

@echo off
SET "status="

:start_test
timeout 5
ping -n 2 -w 700 www.google.com | find "bytes="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.

) ELSE (
    SET internet=Not connected to the internet.
)
IF "%status%"=="%internet%" (
goto :start_test
) ELSE (
goto :teller 
)

:teller
echo ------------------------------------------
echo %internet%
@echo %time%
echo ------------------------------------------
SET "status=%internet%"
goto :start_test

Solution

  • The answer on batch file comparison of variable with constant fails explains in first chapter how to debug a batch file by not double clicking on it, but by opening a command prompt window and running the batch file from within this window to see error messages caused by syntax errors which result in exiting batch file execution.


    SET status="
    

    This line defines the environment variable status with the value ".

    I suppose you wanted to use:

    SET "status="
    

    This line deletes a perhaps existing environment variable status, but you coded this wrong by omitting the first double quote.


    The command PING sets ERRORLEVEL to 0 on success and 1 on an error like target could not be pinged.

    But for some unknown reason you don't want to evaluate the exit code of PING and instead parse output of PING by FIND and evaluate the exit code of FIND.

    The usage of FIND is in my point of view completely unnecessary here.


    The evaluation of the exit code of FIND would be already enough to continue depending on this exit code, but two different strings with spaces are assigned to an environment variable depending on value of ERRORLEVEL.


    One more IF is used to branch depending more or less on exit code of PING.

    Here is your mistake causing exiting batch execution because of a syntax error.

    IF %status%==%internet%(
    

    expands on first comparison to

    IF " == Connected to the internet. (
    

    or to

    IF " == Not connected to the internet. (
    

    Both lines can't be correct processed by Windows command interpreter indicated by a syntax error message output to console which can be read when the batch file is executed from within a command prompt window.

    Comparing strings containing command separator character SPACE requires enclosing both strings to compare in double quotes, i.e. use:

    IF "%status%" == "%internet%" (
    

    But even this would not help here because of environment variable status is defined with a single double quote which results in left string being finally on first comparison """ which results again in a syntax error.

    Note: The double quote characters are included in the string comparison.


    The block below is also interesting:

    IF %status%==%internet%(
    goto :start_test
    ) ELSE (
    goto :teller 
    )
    
    :teller
    

    The ELSE branch is for jumping to the lines which would be executed in any case if the ELSE branch would not exist at all. So the complete ELSE branch is completely unnecessary.


    For all the reasons above, I suggest to simplify the batch code to:

    @echo off
    set "Status=2"
    :ConnectionTest
    %SystemRoot%\System32\ping.exe -n 2 -w 700 www.google.com >nul
    if errorlevel 1 (
        if not "%Status%" == "1" echo %TIME%  Not connected to the internet.
        set "Status=1"
    ) else (
        if not "%Status%" == "0" echo %TIME%  Connected to the internet.
        set "Status=0"
    )
    %SystemRoot%\System32\timeout.exe /T 5 >nul
    goto ConnectionTest
    

    It would be possible here to omit all double quotes and use:

    @echo off
    set Status=2
    :ConnectionTest
    %SystemRoot%\System32\ping.exe -n 2 -w 700 www.google.com >nul
    if errorlevel 1 (
        if not %Status% == 1 echo %TIME%  Not connected to the internet.
        set Status=1
    ) else (
        if not %Status% == 0 echo %TIME%  Connected to the internet.
        set Status=0
    )
    %SystemRoot%\System32\timeout.exe /T 5 >nul
    goto ConnectionTest
    

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    Read also the Microsoft article about using command redirection operators.