none
Windows CMD-Batch file. Determine the success of a ping command. RRS feed

  • Question

  • Hey there, 

    I want to get a message at every single echo the cmd brings out.

    example:

    [Command]:ping 127.0.0.1 -l 32 -n 60

    [CMDecho]:succes

    [CMDecho]:succes

    [CMDecho]:failed -> here i want the cmd to give out another file like a sound file.

    [CMDecho]:succes

    I already tryed it with %ERRORLEVEL% but it opens the file when the "60" pings are over only.

    Greets,   Cerxes Veylon


    Saturday, May 9, 2015 12:34 AM

Answers

  • Hey there, 

    I want to get a message at every single echo the cmd brings out.

    @echo off
    for /L %%a in (1,1,60) do (
      ping 127.0.0.1 -4 -n 1 | find /i "bytes=" > nul
      if ErrorLevel 1 (echo Failed) else (echo Success)
    )

    • Marked as answer by Cerxes Veylon Saturday, May 9, 2015 3:28 PM
    Saturday, May 9, 2015 5:59 AM
  • for /L %%a in (1,160) do (

    is this right? beceause it jumps to "pause" instantly.

    To avoid typographical errors, you should copy my code instead of retyping it.

    Replace
    ping 127.0.0.1 -4 -l %Ares3% -n %Ares4% \ find /i"bytes="> nul
    with
    ping 127.0.0.1 -4 -i %Ares3% -n %Ares4% | find /i  "bytes="> nul

    and

    for /L %%a in (1,160) do
    with
    for /L %%a in (1,1,160) do

    Please remember to mark my reply as "Answer".

    • Marked as answer by Cerxes Veylon Saturday, May 9, 2015 3:28 PM
    Saturday, May 9, 2015 2:08 PM

All replies

  • Use PowerShell Test-Connection.


    \_(ツ)_/

    Saturday, May 9, 2015 5:28 AM
  • Hey there, 

    I want to get a message at every single echo the cmd brings out.

    @echo off
    for /L %%a in (1,1,60) do (
      ping 127.0.0.1 -4 -n 1 | find /i "bytes=" > nul
      if ErrorLevel 1 (echo Failed) else (echo Success)
    )

    • Marked as answer by Cerxes Veylon Saturday, May 9, 2015 3:28 PM
    Saturday, May 9, 2015 5:59 AM
  • Thats what i made now.

    @echo off
    SET /P Ares3=Bytes: 
    SET /P Ares4=Frequency: 

    for /L %%a in (1,160) do (
    ping 127.0.0.1 -4 -l %Ares3% -n %Ares4% \ find /i"bytes="> nul
    if ErrorLevel 1 (echo failed) else (echo success)
    )
    pause

    is this right? beceause it jumps to "pause" instantly.

    Saturday, May 9, 2015 1:09 PM
  • for /L %%a in (1,160) do (

    is this right? beceause it jumps to "pause" instantly.

    To avoid typographical errors, you should copy my code instead of retyping it.

    Replace
    ping 127.0.0.1 -4 -l %Ares3% -n %Ares4% \ find /i"bytes="> nul
    with
    ping 127.0.0.1 -4 -i %Ares3% -n %Ares4% | find /i  "bytes="> nul

    and

    for /L %%a in (1,160) do
    with
    for /L %%a in (1,1,160) do

    Please remember to mark my reply as "Answer".

    • Marked as answer by Cerxes Veylon Saturday, May 9, 2015 3:28 PM
    Saturday, May 9, 2015 2:08 PM
  • for /L %%a in (1,160) do (

    is this right? beceause it jumps to "pause" instantly.

    To avoid typographical errors, you should copy my code instead of retyping it.

    Replace
    ping 127.0.0.1 -4 -l %Ares3% -n %Ares4% \ find /i"bytes="> nul
    with
    ping 127.0.0.1 -4 -i %Ares3% -n %Ares4% | find /i  "bytes="> nul

    and

    for /L %%a in (1,160) do
    with
    for /L %%a in (1,1,160) do

    Please remember to mark my reply as "Answer".

    Thanks it finally works, but its a bit slow, is there any possibility to make it faster? And it would be fine to see the time how long the targed needs to answer.

    btw looks like -n is useless now...
    Saturday, May 9, 2015 3:38 PM
  • It's a bit slow, is there any possibility to make it faster?
    -> Sure. Don't execute the loop 160 times.

    And it would be fine to see the time how long the targed needs to answer.
    -> Agreed. If you post the code to do it then a respondent will gladly help you with the finer points.

    Saturday, May 9, 2015 5:34 PM
  • Here is the time for the response.  If you use PowerShell you can have everything without a lot of fuss. Time to stop using CMD.

    PS C:\scripts> ping www.google.com -n 1

    Pinging www.google.com [216.58.219.228] with 32 bytes of data:
    Reply from 216.58.219.228: bytes=32 time=21ms TTL=55

    Ping statistics for 216.58.219.228:
        Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 21ms, Maximum = 21ms, Average = 21ms


    \_(ツ)_/

    Saturday, May 9, 2015 5:51 PM