Batch Files - Using ping to test network connectivity

前端 未结 8 1111
名媛妹妹
名媛妹妹 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 17:36

    Here's a script that will repeatedly check, and write the time (from system clock) and "internet offline" to a log file at C:\Internet.txt each time the internet goes offline. Unfortunately the latest line in the log file will appear at the end - I don't know how to make it appear at the top ;)

    BTW: I set the wait time (-w) to 20 seconds, because I was using a 3G dongle (with 2G internet) so 20s was often the only way to be sure if the internet was really down or something else was the problem... Feel free to change it to 5000 for 5s, or delete "-w 20000" altogether to leave it at default.

    @echo off
    
    :START
    
    ping -n 4 4.2.2.2 -w 20000 >nul
    
    if %errorlevel% == 1 (
      echo Internet offline >> C:\Internet.txt
      Time /t >> C:\Internet.txt
    )
    
    Timeout /t 30
    @set errorlevel = 0
    
    GOTO START
    

提交回复
热议问题