0

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")
dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • 1
    `if errorlevel 0` is testing for 0 or greater, so it is always True if `%errorlevel%` is `0` or `1` or greater. So the else condition will not happen in your edit. `if errorlevel 1` tests for 1 or greater which is a condition to check for an error code of greater than 0. You can also check success by using `if not errorlevel 1` which is not 1 or greater. – michael_heath Oct 20 '18 at 06:49
  • You have seen it work in my code so you know it returns the correct error level. You are writing complex code for no reason. EG `Set host=%computername%` is completely unnecessary, you use `%computername%` direct. You seem to want to start programs in UNNATURAL ways. See http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 for a brief summary of how to start programs (**HINT don't use `start`**). – CatCat Oct 20 '18 at 20:05
  • Works thanks, removed the Start command and all fine+ used ‘find TTL=‘ to capture address on same subnet – dancingbush Oct 21 '18 at 12:08
  • CMD is text based so slow. When you type `Set host=%computername%` you cause the environment block to be sorted as `%host%` has to be inserted in the CRLF delimted list. `%computername%` is there for you to use. Most programmers won't use inbuilt variables. Many other programmers will use `%host%`. It is a form of obfuscation, it not clear what it is. It is very slow, not clear, and risks conflict in complex environments. And violates the rule that code should be clear and simple. – CatCat Oct 21 '18 at 21:28

1 Answers1

2
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>

CatCat
  • 483
  • 4
  • 5
  • Thanks, interestingly if the following fails I still get the passed prompt while the ping keeps executing:start "Checking Internet Connection- ...."%host% ping www.rotunda.ie echo %Errorlevel% if errorLevel 0 msg * "PC is connected to the Internet" if errorLevel 1 msg * "PC is not connected to the Internet" – dancingbush Oct 19 '18 at 23:29
  • Ping is still running although the error code following it has been executed – dancingbush Oct 19 '18 at 23:45
  • Descending order. From Help `ERRORLEVEL number` *Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.* – CatCat Oct 19 '18 at 23:45
  • But looks like the ping is still running so I am thinking what code has generated an exit code, if any – dancingbush Oct 19 '18 at 23:53
  • If the `If errorlevel 1` triggers, so will the `If errorlevel 0` immediately below it. Suggest `If errorlevel 1 (Echo Failed) else Echo Success`. – michael_heath Oct 20 '18 at 03:11
  • Thanks, however regardless of wether ping fails or passes the ‘ if errorlevel 0 ‘ always executes and doesn’t execute at all for the final internet ping- I have updated code above to demonstrate – dancingbush Oct 20 '18 at 04:55
  • I think its because the errorLevel will always be 0 if the ping command executes, so need to look at the TTL – dancingbush Oct 20 '18 at 05:02
  • 1
    You just won't copy my code and insist on doing unnecessary stuff. – CatCat Oct 20 '18 at 05:13
  • Appreciate it but I am Curious as to why the '| find "TTL" from ping command doesn't execute correctly, also I need the pop up box, and the 'Failed' echo is not visible when i execute your code – dancingbush Oct 20 '18 at 05:43
  • 1
    @CatCat, it is not unnecessary. Please, see [this](https://stackoverflow.com/a/27748080/2861476) – MC ND Oct 20 '18 at 06:05
  • Intrestimg thanks didn’t realise the ping output was different for ipv6 and ipv4 and further more the output on ipv4 subnet, will go back have a look at script – dancingbush Oct 20 '18 at 14:26