Results 1 to 2 of 2

Thread: Ping command and errorlevel

  1. #1
    Member
    Join Date
    Sep 2007
    Location
    Denmark
    Posts
    856
    Thanks
    45
    Thanked 104 Times in 82 Posts

    Ping command and errorlevel

    Im trying to go through at list of computer in batch file to verify they are up and running.
    Im using this little batch snippet for it.


    @echo off

    for /F "tokens=*" %%A in (computerlist.txt) do (
    ping /n 1 "%%A"
    if %errorlevel% == 0 echo %%A online
    if %errorlevel% == 1 echo %%A offline
    )

    however it seems to always report back "online"
    Ive tried putting in echo %errorlevel% right after the ping command and it always response 0

    however if i ctrl+c'ed a previously batch run and run the batch again withing the same command box. Its reports -1047something something

    What am i doing wrong ?

  2. #2
      webmaster's Avatar
    Join Date
    Jun 2010
    Location
    Saint-Petersburg, Russia
    Posts
    70
    Thanks
    12
    Thanked 53 Times in 24 Posts
    try this
    Code:
    @echo off
    setlocal enableextensions enabledelayedexpansion
    
    for /f %%i in (PCS.TXT) do (
       SET bHOSTUP=0
       ping -n 2 %%i |find "TTL=" > NUL && SET bHOSTUP=1
       IF !bHOSTUP! equ 1 (
          CALL :HOSTUP %%i
       ) else (
          CALL :HOSTDOWN %%i 
       )
    )
    GOTO EOF
    
    :HOSTUP
    echo Host UP %1
    GOTO EOF
    
    :HOSTDOWN
    echo Host DOWN %1
    GOTO EOF
    
    :EOF
    exit /B

  3. The Following User Says Thank You to webmaster For This Useful Post:

    SvenBent (29th April 2014)

Similar Threads

  1. Help!!! Need Recompression Command line for Precomp 0.4.3
    By Manjunath in forum Data Compression
    Replies: 9
    Last Post: 13th April 2014, 05:43
  2. Command Line Process Profiling Tool
    By david_werecat in forum Download Area
    Replies: 38
    Last Post: 30th December 2013, 18:12
  3. Replies: 4
    Last Post: 2nd December 2012, 03:55
  4. command-line calculator for Windows?
    By Alexander Rhatushnyak in forum The Off-Topic Lounge
    Replies: 5
    Last Post: 30th January 2012, 00:40
  5. DARK - a new BWT-based command-line archiver
    By encode in forum Forum Archive
    Replies: 138
    Last Post: 23rd September 2006, 21:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •