Reputation: 2255
I'm trying to make a batch script which is supposed to ping a site, log the results, and start a program if the results were negative. This is a modification of original script (not mine), which can be found here. Values of domain, IP, and program variables are for illustrative purposes.
@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
What I'm trying to achieve is this - if anything other than a perfect reply to a ping request is received, the script should start a program. To secure that this happens only then, I've been clearing the result
variable every time, other than on an expected ping response.
Echoing the value of result
keeps returning 1, even after I've emptied it.
Upvotes: 0
Views: 101
Reputation: 2255
Just an addendum to Stephan's post.
In addition to reasons posted in Stephan's post, the code was not working as I had originally planned it to, because the comparisons were failing as well. If there was a successful ping, the result
variable was being set to a string consisting of multiple words, which was breaking the comparison. To avoid this, I had to change every instance of
IF [%result%]==[]
to
IF ["%result%"]==[""]
Thomas Weller's (now deleted) comment was also on the right track - I did need to empty the result
variable in :Start
:
:Start
IF ["%result%"]==[""] (
>>%output% echo -----------
start %program%
)
SET result=
ECHO Pinging %domain%...
This emptying was needed to annul any previous successes and the original set result=1
(in case of consistent failures).
Upvotes: 0
Reputation: 56180
in your line
FOR /F "delims=" %%G in ('ping -4 -n 1 %domain% ^| find "Reply"') DO SET result=%%G
%%G
is either not defined (when the word Reply
doesn't occur), which doesn't touch your Result
variable at all,
or a line like Reply from x.x.x.x : Bytes=32 Time<1ms TTL=128
, which definitively isn't empty.
According to the rest of your code, you probably meant ... DO SET "result="
to unset the variable.
Note: searching for "Reply" is
a) language dependent ("Antwort" on German Windows) and
b) not reliable (think of Reply from localhost: destination address unreachable
).
Better search for TTL=
(even works without a for
loop):
ping -n 1 %IP% | find "TTL=" >nul && set "reply=true" || set "reply=false"
echo %reply%
Upvotes: 1