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

时间:2018-10-19 22:19:15

标签: batch-file output ping

我想根据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")

1 个答案:

答案 0 :(得分: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

请参见if /?,并注意它必须按降序排列。

  

Microsoft Windows [版本10.0.10240](c)2015 Microsoft Corporation。   保留所有权利。

     

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

     

C:\ Windows \ system32> Rem一个始终可用的ip

     

C:\ Windows \ system32> ping 127.0.0.1

     

使用32个字节的数据对127.0.0.1进行ping:来自127.0.0.1的回复:   字节= 32时间<1毫秒TTL = 128从127.0.0.1的答复:字节= 32时间<1毫秒   TTL = 128来自127.0.0.1的回复:字节= 32时间<1ms TTL = 128来自的回复   127.0.0.1:字节= 32时间<1ms TTL = 128

     

127.0.0.1的Ping统计信息:       数据包:已发送= 4,已接收= 4,丢失= 0(丢失0%),大约往返时间(以毫秒为单位):       最小值= 0ms,最大值= 0ms,平均值= 0ms

     

C:\ Windows \ system32>回声0

     

0

     

C:\ Windows \ system32>如果错误级别1回显失败

     

C:\ Windows \ system32>如果错误级别为0,回声成功

     

成功

     

C:\ Windows \ system32> Rem一个不太可能提供的IP

     

C:\ Windows \ system32> ping 125.0.0.255

     

使用32个字节的数据对125.0.0.255执行Ping操作:请求超时。请求   时间到。请求超时。请求超时。

     

125.0.0.255的Ping统计信息:       数据包:已发送= 4,已接收= 0,已丢失= 4(丢失100%),

     

C:\ Windows \ system32>回声1

     

1

     

C:\ Windows \ system32>如果错误级别1回显失败

     

失败

     

C:\ Windows \ system32>如果错误级别0回声成功

     

成功

     

C:\ Windows \ system32>