无法访问目标主机不会导致错误级别 1 [英] Destination Host unreachable does not result in an errorlevel 1

查看:72
本文介绍了无法访问目标主机不会导致错误级别 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在工作中,我们有一些必须加载软件的计算机,通常当我通过以太网电缆将我的笔记本电脑连接到计算机时,我必须将我的 IPv4 地址设置为 10.10.1.99 因为计算机通常将 10.10.1.101 作为 IP 地址,然后我将软件加载到该计算机上.现在有时计算机的 IP 预设错误,例如 10.41.246.7010.42.246.71.

So at work we have some Computers that we have to load softwares onto and usually when I connect my laptop to the computer by an ethernet cable i have to set my IPv4 adress to 10.10.1.99 because the computer usually has 10.10.1.101 as IP adress then I load the software onto that computer. Now sometimes the computer has a wrong IP preset for example 10.41.246.70 or 10.42.246.71.

由于我们没有一种简单快捷的方法来检查计算机的 IP,我写了一个小脚本,将我的笔记本电脑的 IPv4 更改为计算机通常拥有的最常见的 IP,并让它 ping 这些 IP:代码看起来像这样,它检查了大约 8 个 IP:

Since we dont have an easy and fast way to check for what ip the computer has i have written a little scrip that changes the IPv4 of my laptop to the most common IP's the Computers usually have and let it ping those IP's: The code looks like this and it goes through around 8 IP's that it checks:

cls
echo Searching.
netsh interface ip set address "Ethernet" static 10.10.1.99 255.255.255.0 >nul: 2>nul:
ping -w 4 -n 3 10.10.1.101
if !errorlevel!==0 (
    set activeip=10.10.1.101
    goto :ipfound
)

现在这段代码通常工作得很好,在 99% 的情况下,它是我们知道的 8 个 IP 之一.问题是,有时不是请求超时",而是目标主机无法访问",出于某种原因,这似乎不是错误,当我确实无法访问目标主机时,脚本认为它找到了正确的 IP.现在有没有办法解决这个问题,例如添加某种:

Now this code usually works just fine, in 99% of the cases its one of the 8 IP's that we know. The problem is that sometimes instead of "Request timed out" I get "Destination Host unreachable" which for some reason seems to not be an error and when I do get Destination Host unreachable the script thinks it has found the right IP. Now is there a way to go around this for example by adding some kind of:

if output == Destination Host Unreachable (goto next IP)

或者有没有办法告诉脚本目标主机不可达也是一个错误.

or is there a way to tell the script that destination host unreachable is also an error.

感谢所有能以任何方式提供帮助的人.

Thanks to everyone who can help in any way.

推荐答案

因为 Reply from xx.xx.xx.xx: Destination Host Unreachable 在技术上仍然是回复.. :)

Because Reply from xx.xx.xx.xx: Destination Host Unreachable is technically still a reply.. :)

您可以使用 findstr 来操作您的 errorlevel

You can use findstr to manipulate your errorlevel

ping -w 4 -n 3 10.10.1.101 | findstr /i "TTL"
if "%errorlevel%"=="0" echo Success
if "%errorlevel%"=="0" echo Failed

请记住,此处的 errorlevel 是根据 findstr 的结果设置的(您的 findstr 字符串是否与您要求的字符串匹配)找到).

Keep in mind that the errorlevel here is set based on the result of findstr (whether your findstr string matched what you asked it to find).

为了演示,这将返回 0errorlevel 因为满足搜索字符串:

To demonstrate, this will return errorlevel of 0 because the search string was met:

ping -w 4 -n 3 10.10.1.101 | findstr /i "Destination Host Unreachable"
echo %errorlevel%

最后,要修改您的脚本以始终检查实际回复来自而不是目标主机无法访问,然后移动到下一个IP,直到我们找到活动IP,简单地做:

So finally, to ammend your script to always check for actual reply from and not destination host unreachable, and move to the next IP until we find the active IP, simply do:

set "ips=10.10.1.101 10.10.1.102 10.10.1.103 10.10.1.104"
for %%i in (%ips%) do ( 
    ping -w 4 -n 3 %%i | findstr /i "TTL"

    if "!errorlevel!"=="0" (
        set "activeip=%%i"
        goto :ipfound
  )
)

你只需要在我设置我的IP的地方更改你想要的IP列表.

You have to just change the list of IP's you want where I set my IP's.

另外,我假设您已经在某处设置了 EnableDelayedExpansion,看到您正在使用它.

Also, I am assuming that you have EnableDelayedExpansion set somewhere already, seeing that you are using it.

这篇关于无法访问目标主机不会导致错误级别 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆