Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following <code>checklink.cmd</code> program is a good place to start. It relies on the fact that you can do a single-shot ping and that, if successful, the output will contain the line:</p> <pre><code>Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), </code></pre> <p>By extracting tokens 5 and 7 and checking they're respectively <code>"Received"</code> and <code>"1,"</code>, you can detect the success.</p> <pre><code>@setlocal enableextensions enabledelayedexpansion @echo off set ipaddr=%1 :loop set state=down for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do ( if "x%%b"=="xunreachable." goto :endloop if "x%%a"=="xReceived" if "x%%c"=="x1," set state=up ) :endloop echo.Link is !state! ping -n 6 127.0.0.1 &gt;nul: 2&gt;nul: goto :loop endlocal </code></pre> <p>Call it with the name (or IP address) you want to test:</p> <pre><code>checklink 127.0.0.1 checklink localhost checklink nosuchaddress </code></pre> <p>Take into account that, if your locale is not English, you must replace <code>Received</code> with the corresponding keyword in your locale, for example <code>recibidos</code> for Spanish. Do a test ping to discover what keyword is used in your locale.</p> <hr> <p>To only notify you when the state <em>changes</em>, you can use:</p> <pre><code>@setlocal enableextensions enabledelayedexpansion @echo off set ipaddr=%1 set oldstate=neither :loop set state=down for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do ( if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up ) if not !state!==!oldstate! ( echo.Link is !state! set oldstate=!state! ) ping -n 2 127.0.0.1 &gt;nul: 2&gt;nul: goto :loop endlocal </code></pre> <hr> <p>However, as Gabe points out in a comment, you can just use <code>ERRORLEVEL</code> so the equivalent of that second script above becomes:</p> <pre><code>@setlocal enableextensions enabledelayedexpansion @echo off set ipaddr=%1 set oldstate=neither :loop set state=up ping -n 1 !ipaddr! &gt;nul: 2&gt;nul: if not !errorlevel!==0 set state=down if not !state!==!oldstate! ( echo.Link is !state! set oldstate=!state! ) ping -n 2 127.0.0.1 &gt;nul: 2&gt;nul: goto :loop endlocal </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload