在批处理文件中评估 ping 结果

Evaluating ping outcome in batch file

我想根据 ping 命令的结果显示一个弹出消息,但是无论 ping 失败还是通过,ping 成功消息都会执行:

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 已更新代码-但是无论 ping 是否成功,if errorlevel 0 条件都会执行,我认为这可能是因为 ping 命令成功执行所以 errorLevel = 0,无论结果如何即 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")
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

参见if /?,注意需要降序排列。

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>