Batch ERRORLEVEL ping response

前端 未结 13 1517
走了就别回头了
走了就别回头了 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-14 23:52

    ping has an errorlevel output value. Success is 0, failure is 1. Just do this:

    C:\>ping 4.2.2.2
    
    Pinging 4.2.2.2 with 32 bytes of data:
    
    Reply from 4.2.2.2: bytes=32 time=28ms TTL=57
    Reply from 4.2.2.2: bytes=32 time=29ms TTL=57
    Reply from 4.2.2.2: bytes=32 time=30ms TTL=57
    Reply from 4.2.2.2: bytes=32 time=29ms TTL=57
    
    Ping statistics for 4.2.2.2:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 28ms, Maximum = 30ms, Average = 29ms
    
    C:\>echo %errorlevel%
    0
    
    C:\>ping foo.bar
    Ping request could not find host foo.bar. Please check the name and try again.
    
    C:\>echo %errorlevel%
    1
    

    As you can see there is no need for all this scripting overkill.

    0 讨论(0)
  • 2020-12-14 23:53
    ping 198.168.57.98 && echo Success || echo failed
    
    0 讨论(0)
  • 2020-12-14 23:53

    I liked the concept of the FIND in the ping results but why not just FIND the Reply from the Address being pinged?

    In the example below I enter an IP address as a variable, PING that IP, then look for that variable in the reply string, using the FIND Command. If the Reply String contains anything other than the correct IP it reports failure.

    If you want you can just read the value of ERRORLEVEL from the FIND. That will give you a reliable value to work with.

    @echo off
    Set /P IPAdd=Enter Address:
    cls
    ping %IPAdd% | find "Reply from %IPAdd%:"
    if not errorlevel 1 set error=success
    if errorlevel 1 set error=failure
    cls
    echo Result: %error%
    pause
    
    0 讨论(0)
  • 2020-12-14 23:54

    I needed to reset a wifi connection because it has issues. This was my quick solution.

    @echo off
    Rem Microsoft Windows 10 ping test to gateway.
    Rem Run batch file from an administrative command prompt.
    
    cls
    :starting
    Rem Send one ping to the gateway.  Write the results to a file.
    ping 192.168.1.1 -n 1 > pingtest.txt
    
    Rem Search for unreachable in the file. 
    c:\windows\system32\findstr.exe "unreachable" pingtest.txt
    
    Rem errorlevel 0 reset the adapter if 1 then wait 10 minutes and test again
    if %errorlevel%==1 goto waiting
    
    Rem unreachable was found reset the adapter.
    
    Rem write the date and time the reset was done.
    echo Reset date: %date% time: %time% >> resettimes.txt
    
    Rem issue netsh interface show interface to find your adapter's name to reset
    Rem my adapter is "wi-fi"
    
    netsh interface set interface "wi-fi" disable
    timeout /t  5
    netsh interface set interface "wi-fi" enable
    :waiting
    echo "It is online waiting 10 minutes"
    timeout /t  600
    goto starting
    
    0 讨论(0)
  • 2020-12-14 23:56

    First of all

    >@echo off
    >for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
    >echo %MATCHES%
    

    Does not work. If it won't fail, it will detect 0%, because it has 0%. If it fails, does not work either, because it will have 100% loss, which means, it found the 0% loss part behind the 10 10(0% loss)

    Have it detect for 100% loss like so:

    >for /f %%i in ('ping  -n 1 -l 1 %pc% ^| find /c "(100%% loss)"') do SET check=%%i
    

    Errorlevel might be a bit messed up, but it works like a charm:

    >if '%check%'=='1' goto fail
    >if '%check%'=='0' echo %pc% is online.&goto starting
    

    1 means it failed 0 means it succeeded

    In my script is use links. Goto fail will go to :fail in my script which will message me that %pc% (which I'll have the user input in the beginning) is offline and will go for another run.

    >:fail
    >color 0c
    >title %pc% is offline
    >echo %pc% is offline
    >PING -n 6 127.0.0.1>nul
    >goto choice
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-14 23:59

    Testing for 0% loss may give a false positive, in this scenario: Let's say you normally have a network drive on some_IP-address, and you want to find out whether or not it's on.

    If that drive is off, and you ping some_IP-address, the IP address from which you ping, will respond:
    Answer from your_own_IP-address: target host not reachable
    ... 0% loss

    You might be better off using if exist or if not exist on that network location.

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