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

问题描述 投票:0回答:2

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

编辑已更新代码-但是无论 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")
batch-file output ping
2个回答
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 字节数据 Ping 127.0.0.1:来自 127.0.0.1 的回复: 字节=32次<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

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

C:\Windows\system32>Echo 0

0

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

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

成功

C:\Windows\system32>Rem 一个不太可能可用的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>如果错误级别 1 回显失败

失败

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

成功

C:\Windows\system32>


0
投票

测试由

ERRORLEVEL
设置的
ping
是没有用的,因为在某些情况下,即使没有从远程主机收到任何有效回复,它也会将
ERRORLEVEL
设置为 0(成功)!!!

但是,下面描述的方法是可移植的并且适用于:

  1. 翻译成所有其他语言,例如:“ms”、“TTL”、“回复”、“丢失”、“无法到达”、“已收到”等...,包括俄语和中文。
  2. Ping IPv4 地址。
  3. Ping IPv6 地址。
  4. 发生错误“一般故障”的情况。
  5. Ping 一个地址,返回 “请求超时。”
  6. Ping 一个地址,返回 “目标主机无法访问。”
  7. Ping 可解析的字母数字主机。
  8. Ping 无法解析的字母数字主机,这会导致返回 “Ping 请求无法找到主机 UnresolvableHost...” 错误。
@echo off

for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")

if %res% GEQ 3 (
    echo SUCCESS.
) else (
    echo FAILURE.
)

如果将上面的代码保存到名为例如

pingtest.bat
,你可以这样使用它:

pingtest 192.168.1.1

此方法利用了字符串中存在的等号

bytes=
,这些等号不受翻译为其他语言的影响,并且不会出现在不成功的 ping/echo 结果中。

此外,将开关

-n 1
修改为例如
-n 6
允许您在变量
%res%
中获得反映连接质量的定性结果。

使用下面的代码,您可以标准化变量

%res%
中的结果,使其在失败时变为
0
...以及在成功时 - 成功 ping 的数量:

@echo off

for /F %%A in ('ping %1 -n 6 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")

if %res% GEQ 3 (
    set /A "res=%res%-2"
) else (
    set "res=0"
)

echo The number of successful pings is %res%

要暂停程序直到成功 ping 通主机,只需像这样循环调用它:

@echo off

:loop
for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")

if %res% LSS 3 goto :loop
© www.soinside.com 2019 - 2024. All rights reserved.