Batch file 评估批处理文件中的ping结果

Batch file 评估批处理文件中的ping结果,batch-file,output,ping,Batch File,Output,Ping,我想根据ping通信的结果显示一条弹出消息,无论ping是否失败或通过,只要执行ping was successful消息: 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 hos

我想根据ping通信的结果显示一条弹出消息,无论ping是否失败或通过,只要执行ping was successful消息:

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)
编辑已更新代码-但是,无论ping是否成功,如果执行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")
请参见
if/?
,并注意它需要按降序排列

微软视窗[Version 10.0.10240](c)2015微软公司。 版权所有

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

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

C:\Windows\system32>ping 127.0.0.1

使用32字节数据ping 127.0.0.1:来自127.0.0.1的回复: bytes=32 timeRem不太可能可用的ip

C:\Windows\system32>ping 125.0.0.255

使用32字节数据ping 125.0.0.255:请求超时。要求 超时。请求超时。请求超时

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

C:\Windows\system32>Echo 1

1

C:\Windows\system32>如果errorlevel 1回显失败

失败

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

成功

C:\Windows\system32>

请参见
if/?
,并注意它需要按降序排列

微软视窗[Version 10.0.10240](c)2015微软公司。 版权所有

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

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

C:\Windows\system32>ping 127.0.0.1

使用32字节数据ping 127.0.0.1:来自127.0.0.1的回复: bytes=32 timeRem不太可能可用的ip

C:\Windows\system32>ping 125.0.0.255

使用32字节数据ping 125.0.0.255:请求超时。要求 超时。请求超时。请求超时

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

C:\Windows\system32>Echo 1

1

C:\Windows\system32>如果errorlevel 1回显失败

失败

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

成功

C:\Windows\system32>



谢谢,有趣的是,如果以下操作失败,当ping继续执行时,我仍然会收到通过的提示:开始“检查Internet连接-…”%host%ping www.rotunda.ie echo%Errorlevel%if Errorlevel 0 msg*“PC连接到Internet”if Errorlevel 1 msg*“PC未连接到Internet”Ping仍在运行,但随后的错误代码已按降序执行。从“帮助”
ERRORLEVEL number
中,如果上次运行的程序返回的退出代码等于或大于指定的数字,则指定一个真实条件。您不会复制我的代码,坚持做不必要的事情。@CatCat,这不是不必要的。请参阅,谢谢,有趣的是,如果以下操作失败,在ping继续执行时,我仍然会收到通过的提示:开始“检查Internet连接-…”%host%ping www.rotunda.ie echo%Errorlevel%if Errorlevel 0 msg*“PC连接到Internet”if Errorlevel 1 msg*“PC未连接到Internet”Ping仍在运行,但随后的错误代码已按降序执行。从“帮助”
ERRORLEVEL number
中,如果上次运行的程序返回的退出代码等于或大于指定的数字,则指定一个真实条件。您不会复制我的代码,坚持做不必要的事情。@CatCat,这不是不必要的。请参阅
如果errorlevel 0
测试的是0或更高,那么如果
%errorlevel%
0
1
或更高,则始终为真。因此,else条件将不会在您的编辑中发生<代码>如果错误级别1
测试1或更大,这是检查错误代码是否大于0的条件。您还可以使用
if not error level 1
检查成功,该值不是1或更高。您已经在我的代码中看到它工作,因此您知道它返回正确的错误级别。您无缘无故地编写复杂的代码。例如
Set host=%computername%
完全没有必要,您可以直接使用
%computername%
。你似乎想以不自然的方式启动程序。有关如何启动程序的简要摘要,请参阅(提示不要使用
start
)。谢谢您的帮助,删除了start命令,并使用“find TTL=”捕获同一子网上的地址。cmd基于文本的速度非常慢。键入
Set host=%computername%
时,会导致环境块被排序为
%host%
,必须将其插入CRLF删除列表中<代码>%computername%供您使用。大多数程序员不会使用内置变量。许多其他程序员将使用
%host%
。这是一种混淆,不清楚它是什么。这是非常缓慢的,不明确的,并且在复杂的环境中存在冲突的风险。并且违反了代码应该清晰和简单的规则。
如果errorlevel 0
测试的是0或更高,那么如果
%errorlevel%
0
1
或更高,则总是正确的。因此,else条件将不会在您的编辑中发生<代码>如果错误级别1测试1或更大,这是检查错误代码是否大于0的条件。您还可以使用
if not error level 1
检查成功,该值不是1或更高。您已经在我的代码中看到它工作,因此您知道它返回正确的错误级别。您无缘无故地编写复杂的代码。例如
设置主机=%computername
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