batch-fileoutputping

Evaluating ping outcome in batch file


I want to present a pop up message based on the outcome of a ping comannd, how ever regardless of whether the ping fails or passes the ping was successful message is executed:

start "Checking your PC is on the network- ...."%host% ping localhost |find "TTL=">nul &&  (msg "%username%": Ping to Local Host Failed)  || (msg "%username%" Ping to Local host Succesful)  

start  "Checking Gateway for computer - ..."%host%  ping 10.89.24.1 -t -a -n 5 |find "TTL=">nul &&  (msg "%username%": Ping to Gatewway Failed) || (msg "%username%" Ping to Gateway Succesful) 



start "Checking Apex Server for computer -...."%host% ping 193.120.187.44 -t -a -n 5 |find "TTL=">nul &&  (msg "%username%": Ping to Apex Server Failed) || (msg "%username%" Ping to Apex Server Succesful)


start "Checking Intranet Connection- ...."%host% ping 10.89.208.9 -t -a -n 5 |find "TTL=">nul &&  (msg "%username%": Ping to Intranet Failed) || (msg "%username%" Ping to Intranet Successful)

EDIT Have updated code- however the if errorlevel 0 condition executes regardless of whether the ping is successful or not, I think this may be because the ping command executes successfully so the errorLevel = 0, regardless of the outcome i.e. TTL

@echo off

set host=%COMPUTERNAME%

rem color 0b



timeout 4

start "STEP 1 Checking your PC is on the network- ...."%host% ping localhost -n 4 >NUL
echo %Errorlevel%

if errorLevel 0 (
    msg * "PC is connected to the network"
) else (

 msg * "PC is not connected to the network, check PC is plugged into network point"

)

timeout 4

start  "STEP 2 Checking Gateway for computer - ..."%host%  ping 10.89.24.1 -n 4 >NUL
echo %Errorlevel%
if errorLevel 0 (
 msg * "PC is connected to the Router/Gateway"
) else (
 msg * "PC is not connected to the Router/Gateway"
)


timeout 4
start "STEP 3Checking Apex Server for computer -...."%host% ping 193.120.187.44 -n 4 >NUL
echo %Errorlevel%
if errorLevel 0 (msg * "PC is connected to Apex, chck the APex application  and/or raise an iASSIT"
) else (

 msg * "PC is not connected to the Apex, network down"
)

timeout 4



start "STEP 4 Checking Intranet Connection- ...."%host% ping 10.89.208.9 -n 4 >NUL
echo %Errorlevel%
if errorLevel 0 (msg * "PC is connected to the Intranet"
) else (
 msg * "PC is not connected to the Intranet")

timeout 4


start "STEP 5 Checking Internet Connection- ...."%host% ping www.rotunda.ie -n 4 >NUL
echo %Errorlevel%
if errorLevel 0 (msg * "PC is connected to the Internet"
) else (
 msg * "PC is not connected to the Internet")

Solution

  • Rem An ip that is always available
    ping 127.0.0.1
    Echo %Errorlevel%
    If errorlevel 1 Echo Failed
    If errorlevel 0 Echo Sucess
    
    Rem An ip that is unlikely to be available
    ping 125.0.0.255
    Echo %Errorlevel%
    If errorlevel 1 Echo Failed
    If errorlevel 0 Echo Sucess
    

    See if /? and note that it needs to be in descending order.

    Microsoft Windows [Version 10.0.10240] (c) 2015 Microsoft Corporation. All rights reserved.

    C:\Windows\system32>"C:\Users\David Candy\Desktop\TestN.bat"

    C:\Windows\system32>Rem An ip that is always available

    C:\Windows\system32>ping 127.0.0.1

    Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

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

    C:\Windows\system32>Echo 0

    0

    C:\Windows\system32>If errorlevel 1 Echo Failed

    C:\Windows\system32>If errorlevel 0 Echo Sucess

    Success

    C:\Windows\system32>Rem An ip that is unlikely to be available

    C:\Windows\system32>ping 125.0.0.255

    Pinging 125.0.0.255 with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out.

    Ping statistics for 125.0.0.255: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

    C:\Windows\system32>Echo 1

    1

    C:\Windows\system32>If errorlevel 1 Echo Failed

    Failed

    C:\Windows\system32>If errorlevel 0 Echo Success

    Success

    C:\Windows\system32>