【问题标题】:Ping Test Using Bat File - Trouble with errorlevel使用 Bat 文件进行 Ping 测试 - 错误级别的问题
【发布时间】:2014-02-10 07:18:07
【问题描述】:

我正在使用批处理文件设置 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)

【问题讨论】:

  • 是局域网上的每台计算机都分配了一个静态 IP 还是动态的? DHCP 开启了吗?如果是这样,我建议使用计算机名称而不是其 IP 地址。 ping <computername> 这样您就可以查找特定的计算机,而不是分配了该 IP 地址的任何设备。
  • 动态测试,我计划运行的将是静态测试。我想使用这些 IP,以防在任何时间点出现名称解析问题。

标签: batch-file ping


【解决方案1】:

虽然我无法复制您的问题,但我确实对您的脚本有一些建议。 (有关此问题的问题,请参阅我的评论)

  1. 创建变量时封装范围。 setlocalendlocal
  2. 退出脚本时,使用 /b 标志不会终止调用者的命令提示符。
  3. nul 不为空。

例子():

@echo off
setlocal
cls

set "IPaddress=www.google.com"
call :PingVerbose "%IPaddress%"
call :PingVerbose "www.yahoo.com"
call :PingVerbose "www.microsoft.com"

pause>nul
endlocal
exit /b 0

:Ping <Address>
ping "%~1" -n 1 >nul
exit /b %ErrorLevel%

:PingVerbose <Address>
call :Ping %1 && echo %~1 is Online || echo %~1 is Offline
exit /b %ErrorLevel%

【讨论】:

  • 这是一个非常深思熟虑的回应,应该被赞成。我会解释一下“功能”是如何工作的,但也许它足够直观?
【解决方案2】:

虽然我也无法复制您的问题,并且也有一个改进您的脚本的建议 -

@echo off & cls

set addresses=10.1.1.666 10.124.412.14 10.7.254.1

for %%a in (%addresses%) do ping %%a -n 1 > nul || echo %%a is offline

请注意,|| 之后的命令只有在 ping 设置了错误级别时才会执行。

【讨论】:

    【解决方案3】:

    当您在子网中 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
    )
    

    【讨论】:

      【解决方案4】:

      考虑到其他人提到的内容,我想说明一个人可能需要如何完成上面显示的所有内容以及使用修改后的变量,例如循环中的计数器。

      注意:使用“setlocal enabledelayedexpansion”允许在循环等中使用修改后的变量。

      @echo off
      setlocal enabledelayedexpansion
      
      REM List of systems to check
      set list=www.google.com www.yahoo.com www.notasite.com
      
      set /a failed=0
      set /a passed=0
      set /a count=0
      
      echo PingTest Servers on %list% :
      
      (for %%a in (%list%) do ( 
          set /a "count+=1"
          call :PingVerbose %%a && set /a "passed=!passed!+1" || set /a "failed=!failed!+1"
      ))
      
      echo ------------
      echo Result: %passed% of %count% systems are pingable
      pause
      endlocal
      exit /b 0
      
      :Ping <Address>
      ping "%~1" -n 1 >NUL
      exit /b %ErrorLevel%
      
      :PingVerbose <Address>
      call :Ping %1 && echo %~1 - [ONLINE] || echo %~1 - [OFFLINE] 
      exit /b %ErrorLevel%
      

      输出:

      PingTest Servers on www.google.com www.yahoo.com www.notasite.com :
      www.google.com - [ONLINE]
      www.yahoo.com - [ONLINE]
      www.notasite.com - [OFFLINE]
      ------------
      Result: 2 of 3 systems are pingable
      Press any key to continue . . .         
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        相关资源
        最近更新 更多