【问题标题】:Checking host reachability on windows command line在 Windows 命令行上检查主机可达性
【发布时间】:2015-04-19 16:27:37
【问题描述】:

如果主机可访问,我如何签入批处理文件?问题是 ping 不仅在成功时返回 0,而且在 Destination host unreachable 错误时返回 0:

C:\>ping 192.168.1.1 -n 1

Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=3ms TTL=64

Ping statistics for 192.168.1.1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 3ms, Maximum = 3ms, Average = 3ms

C:\>echo %errorlevel%
0

C:\>ping 192.168.1.105 -n 1

Pinging 192.168.1.105 with 32 bytes of data:
Reply from 192.168.1.102: Destination host unreachable.

Ping statistics for 192.168.1.105:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

C:\>echo %errorlevel%
0

有什么方法可以使用ping 或任何其他内置 Windows 工具来实现吗?如果可能的话,我宁愿不安装任何东西......

【问题讨论】:

    标签: windows batch-file cmd network-programming ping


    【解决方案1】:

    下一个代码 sn-p 应该可以工作:

    set "_ip=localhost"
    set "_ip=192.168.1.1"
    ping %_ip% -n 1 -4 | find /i "TTL=">nul
    if errorlevel 1 (
        echo ping %_ip% failure
    ) else ( 
        echo ping %_ip% success
    )
    
    • -4 强制使用 IPv4(例如 ping localhost 回复 IPv6 而没有 TTL= 输出)
    • | find /i "TTL=" as find 命令引发 errorlevel
    • >nul 抑制不需要的输出

    【讨论】:

      【解决方案2】:

      做这样的事情:

      PING 192.168.1.1 -n 1 | FIND /I "Reply from "
      

      然后你可以检查错误级别

      编辑:为了适应“目标主机无法访问的错误,您可以这样做:

      PING 192.168.1.1 -n 1 | FIND /I /V "unreachable" | FIND /I "Reply from "
      

      【讨论】:

      • 不错,虽然必须找到“TTL”而不是“Reply from”,因为“Destination host unreachable”也有“Reply from”;)
      • Reply from 192.168.1.102: Destination host unreachable. 呢?
      • LOL - 这行得通。虽然我也更喜欢TTL= 方法。 (它更短且与语言无关)
      【解决方案3】:

      我认为你需要tracert。如果找不到主机,则将错误级别设置为1。并且应该使用与ping相同的端口。-h 2是为了减少等待时间,因为不需要完整的路由。

      tracert -h 2 somehost.com >nul 2>nul && (
        echo reachable host
      ) || (
        echo unreachable host
      )
      

      编辑:要在括号中打印正确的错误级别,您需要延迟扩展。这是更高级的示例:

      @echo off
      
      setlocal enableDelayedExpansion
      
      echo checking google.com
      tracert -h 1 google.com >nul 2>nul && (
        echo reachable host
        echo !errorlevel!
        rem prevent execution of negative condition
        color 22
      ) || (
        echo unreachable host 
        echo !errorlevel!
      )
      
      
      echo checking asdasdjjakskwkdhasdasd
      tracert -h 1 asdasdjjakskwkdhasdasd >nul 2>nul && (
        echo reachable host#
        echo !errorlevel!
         rem prevent execution of negative condition
         color
      ) || (
        echo unreachable host#
        echo !errorlevel!
      )
      

      【讨论】:

      • @burtek - 如果它可访问和不可访问?
      • @burtek - 尽量不要在括号中打印错误级别,而是在它们之后。或者在两行中执行这些:tracert nosuchhostecho %errorlevel%
      • 好的,所以它非常适用于域,但不适用于 IP 地址。对于tracert nosuchhost,错误级别为1,对于tracert google.pl,错误级别为0,但对于现有和不存在的IP地址,我得到错误级别0。此外,在不存在IP地址的情况下运行tracert会导致pastebin.com/DieTCw15
      • @burtek - 没有用 IP 地址测试它...会检查它。
      猜你喜欢
      • 2015-12-07
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多