CMD如果ping成功,请执行此操作 [英] CMD if ping successful do this

查看:52
本文介绍了CMD如果ping成功,请执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种我无法理解的情况,我正在尝试做一个批处理文件,他在其中循环200次,并且在每次循环中ping主机,如果ping成功,则他执行命令.这是我得到的:

  @echo关闭对于/L %% N IN(1、1、200)DO(ping -n 1 192.1.22.%% N如果不是ERRORLEVEL 1(设置pingresult = true转到完成))设置pingresult = false:完毕如果%pingresult%== true(回声皮卡丘) 别的 (回声脱机!") 

它不起作用.

解决方案

感谢aschipfl我找到了答案!

您只需要使用 |在 ping 命令后找到"TTL ="> nul ,因为这样,如果ping成功,他将放下TTL和 |找到"TTL => nul 将对其进行验证.这是我从网站上获得解决方案的示例.

  ping -n 1 192.168.1.1 |找到"TTL => nul如果错误级别为1(回声主机无法访问) 别的 (回显主机可达) 

i have situation that i can not understand, i am trying to do a batch file where he loops 200 times and in each loop he pings a host, if the ping is successful he does a command. Here is what i got:

    @echo off
for /L %%N IN (1, 1, 200) DO (
    ping -n 1 192.1.22.%%N
    if not ERRORLEVEL 1 (
        set pingresult=true
        goto done
    )
)

set pingresult=false
:done
if %pingresult% == true (
 echo Pikachu


) else (
  echo "Offline!"
)

it doesn't work.

解决方案

Thanks to aschipfl I discovered the answer!

you just have to use | find "TTL=" >nul after the ping command because this way if the ping was successful he devolve a TTL and the | find "TTL=" >nul will grep it for validation. This is the example from the website I got the solution.

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

这篇关于CMD如果ping成功,请执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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