batch-file - 使用 Bat 文件进行 Ping 测试 - 错误级别问题

标签 batch-file ping

我正在使用批处理文件设置 LAN ping 测试。我的代码对网站非常有用,但对本地 IP 来说却很奇怪。我正在 3 台我知道其 IP 的计算机上运行 ping 测试。无论我拔掉哪一个,当我运行下面的代码时,所有三台计算机上的 %errorlevel% 始终为 0。它永远不会像在网站上那样等于 1。我该如何解决这个问题?

@echo off
cls
Set IPaddress=www.google.com
PING %IPaddress% -n 1
 call :PingTest

Set IPaddress=www.yahoo.com
PING %IPaddress% -n 1
 call :PingTest

Set IPaddress=www.unabletoping.com
PING %IPaddress% -n 1
 call :PingTest

pause > null
exit

:PingTest
IF %errorlevel% EQU 1 (echo "Server is Offline") else (GOTO:EOF)

最佳答案

当您在您的子网中 ping 一个不可访问的地址时,您会得到一个“无法访问”的答案,发送了 1 个数据包,接收了 1 个打包的数据包,丢失了 0 个数据包。未设置错误级别。

当您从您的子网中 ping 一个不可访问的地址时,您会得到一个“超时”的回答,发送了 1 个数据包,接收了 0 个数据包,丢失了 1 个数据包。设置了错误级别。

并且,您可以 ping 一台事件的机器,丢失数据包并获得错误级别

并且,您可以 ping 一台事件/非事件机器,获取 TTL 过期并且没有错误级别

更好,检查 ping 响应的内容。

ping -n 1 192.168.1.1 | find "TTL=" >nul
if errorlevel 1 (
    echo host not reachable
) else (
    echo host reachable
)

关于batch-file - 使用 Bat 文件进行 Ping 测试 - 错误级别问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245545/

相关文章:

powershell - "Cannot bind parameter because Param2 is specified more than once"

windows - 从批处理文件中获取默认网关

ping - 在 MacOS 上请求 icmp_seq 超时

c - ping 执行如何计算往返时间?

Java运行时exec不执行批处理脚本

powershell - 检查 HTTP 端口是否在 Windows 上打开

r - 从 R 控制台执行批处理文件 (psexec.exe)

python - Ansible跳过无法访问的主机

iphone - 服务器与 iPhone Ping

c# - 如何使用 C# 并行执行多个 "Pings"