Batch ERRORLEVEL ping response

前端 未结 13 1503
走了就别回头了
走了就别回头了 2020-12-14 23:05

I\'m trying to use a batch file to confirm a network connection using ping. I want to do batch run and then print if the ping was successful or not. The problem is that it a

相关标签:
13条回答
  • 2020-12-15 00:02

    A more reliable ping error checking method:

    @echo off
    set "host=192.168.1.1"
    
    ping -n 1 "%host%" | findstr /r /c:"[0-9] *ms"
    
    if %errorlevel% == 0 (
        echo Success.
    ) else (
        echo FAILURE.
    )
    

    This works by checking whether a string such as 69 ms or 314ms is printed by ping.

    (Translated versions of Windows may print 42 ms (with the space), hence we check for that.)

    Reason:

    Other proposals, such as matching time= or TTL are not as reliable, because pinging IPv6 addresses doesn't show TTL (at least not on my Windows 7 machine) and translated versions of Windows may show a translated version of the string time=. Also, not only may time= be translated, but sometimes it may be time< rather than time=, as in the case of time<1ms.

    0 讨论(0)
提交回复
热议问题