Simple batch for checking internet connectivity and setting environment variable %internet

前端 未结 4 655
无人共我
无人共我 2020-12-18 05:16

I want to check internet connection, when it fails I want to set %internet% to not connected. And if it works to connected.

echo ch         


        
4条回答
  •  [愿得一人]
    2020-12-18 05:16

    Another option...

    ping -n 2 -w 700 10.11.1.1 | find "bytes="
    IF %ERRORLEVEL% EQU 0 (
        SET internet=Connected to the internet.
    ) ELSE (
        SET internet=Not connected to the internet.
    )
    echo %internet%
    

    This works well because...

    • Sometimes the return for ping is Reply from 10.11.1.106: Destination host unreachable. and since it has an errorlevel of zero this fails. (at least for me)
    • ping -n 2 will still pass if there happens to be a dropped packet.

提交回复
热议问题