2017-09-16 33 views
0

我试图制作一个批处理脚本,该脚本应该可以对站点执行ping操作,记录结果并在结果为负数时启动程序。这是对原始脚本(不是我的)的修改,可以找到here。域,IP和程序变量的值仅用于说明目的。设置为空后批处理脚本中未清空的变量

@echo off 
cls 

set domain=testsite.com 
set IP=133.78.17.101 
set program=c:\windows\notepad.exe 
set output=c:\log.txt 
set result=1 

:Start 
IF [%result%]==[] (
    >>%output% echo ----------- 
    start %program% 
) 
ECHO Pinging %domain%... 
FOR /F "delims=" %%G in ('ping -n 1 %domain% ^| find "Reply"') DO SET result=%%G 
IF NOT [%result%]==[] (
    goto Success 
) ELSE (
    goto TryAgain 
) 

:TryAgain 
ECHO %domain% unreachable. Trying again... 
FOR /F "delims=" %%G in ('ping -n 1 %domain% ^| find "Reply"') DO SET result=%%G 
IF NOT [%result%]==[] ( 
    goto Success2 
) ELSE (
    goto TryIp 
) 

:TryIp 
ECHO %domain% unreachable. Pinging %ip%... 
FOR /F "delims=" %%G in ('ping -n 1 %IP% ^| find "Reply"') DO SET result=%%G 
IF NOT [%result%]==[] (
    goto SuccessDNS 
) ELSE (
    goto TestInternet 
) 

:TestInternet 
ECHO %ip% unreachable. Testing internet connection. 
FOR /F "delims=" %%G in ('ping -n 1 www.google.com ^| find "Reply"') DO SET result=%%G 
IF NOT [%result%]==[] (
    goto Success3 
) ELSE (
    goto NetDown 
) 

:Success 
>>%output% ECHO Connected 
>>%output% echo %date% %time% %result% 
ping -n 3 127.0.0.1 > nul 
goto Start 

:Success2 
>>%output% ECHO Connected with packet loss. 
>>%output% echo %date% %time% %result% 
set result= 
ping -n 3 127.0.0.1 > nul 
goto Start 

:Success3 
>>%output% ECHO Domain %domain% not reachable. Connected via IP. 
>>%output% echo %date% %time% %result% 
set result= 
ping -n 3 127.0.0.1 > nul 
goto Start 

:SuccessDNS 
>>%output% ECHO DNS problem. 
>>%output% echo %date% %time% %result% 
set result= 
ping -n 3 127.0.0.1 > nul 
goto Start 

:NetDown 
>>%output% ECHO No internet connection. 
>>%output% echo %date% %time% %result% 
set result= 
ping -n 3 127.0.0.1 >nul 
goto Start 

我试图实现这一目标是 - 如果收到不是一个完美的答复ping请求的任何其他的脚本应该启动一个程序。为了确保这种情况发生,我每次都清除result变量,而不是预期的ping响应。

即使在清空它之后,回应result的值仍会返回1。

+2

'%result%'和'%result'不一样。你有一个错误的'IF NOT [%result] == []('lines。 – Stephan

+2

'%result%'改变'%result'的所有实例并重试! – Compo

+0

@Stephan正确,纠正它,但仍然不按照我的预期工作 – FiddlingAway

回答

1
在您的线路

FOR /F "delims=" %%G in ('ping -4 -n 1 %domain% ^| find "Reply"') DO SET result=%%G 

%%G

要么没有定义(当字Reply不发生),其不接触的Result变量在所有,
或类似Reply from x.x.x.x : Bytes=32 Time<1ms TTL=128一条线,其肯定不是空的。根据您的其他代码,您可能指的是... DO SET "result="来取消设置变量。

注:搜索 “回复” 是
一)在德语Windows语言依赖( “Antwort”)和
B)不可靠(认为Reply from localhost: destination address unreachable)。
更好的搜索TTL=(即使工作没有for环路):

ping -n 1 %IP% | find "TTL=" >nul && set "reply=true" || set "reply=false" 
echo %reply% 
+0

实际上,'DO SET result = %% G'在代码中,因为我需要将'result'记录到一个带有>>%output%ECHO(%result%'的文件中。但是,如果没有'Reply'(或TTL,正如你所建议的那样),'result'不会被改变/定义。这就是代码不能按照我期望的那样工作的原因之一。谢谢,指出这一点。 – FiddlingAway

0

只是一个增编Stephan's post

除了在Stephan的帖子中发布的原因之外,代码没有像我最初计划的那样工作,因为比较失败了。如果ping成功,则result变量将被设置为由多个单词组成的字符串,这将打破比较。为了避免这种情况,我不得不的

IF [%result%]==[] 

每个实例更改为

IF ["%result%"]==[""] 

托马斯·韦勒(现已删除)评论也是正确的轨道上 - 我没有需要清空result变量在:Start

:Start 
IF ["%result%"]==[""] (
    >>%output% echo ----------- 
    start %program% 
) 
SET result= 
ECHO Pinging %domain%... 

需要这排空宣布任何以前的成功和原set result=1(如果一致失败)。