Create batch file to ping address, then execute task based on result

I need to create a batch file, in Windows, that will ping an IP address, and if the host is alive, do nothing, but if the host is not responding, execute a DNS command using the dnscmd.exe file.

I have the dnscmd.exe syntax worked out and that works fine when I execute it manually or in a simple batch.

Where I need help is coming up with the right syntax to execute a ping, and depending on the result, either do nothing, or execute the dnscmd command string.

I think I've figure out the best way to determine success or failure of the ping. If you key in on checking for the appearance of "TTL=" that will tell you success or failure. I just can't come up with a way to execute the ping, look for TTL= and then perform either an exit or run my dnscmd command.

Thanks.
Vjz1Asked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
You can adjust the ping parms as needed, but here is one approach.

ping -n 1 -l 0 hostname | find "TTL=" > NUL || (
    rem Put logic you want to exec here for node down
)

Open in new window


»bp
0
 
Vjz1Author Commented:
That works. Thanks.
0
 
Vjz1Author Commented:
Thanks.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Bill PrewCommented:
Welcome.


»bp
0
 
Vjz1Author Commented:
Hey, so actually I just realized, I need it to do the opposite. If the "TTL=" is there, I need it to do nothing. It's when it's not present, I need to execute an action.

Can you help?
0
 
Bill PrewCommented:
That is what that code does.  After a command executes you can use && and/or || to perform logic based on success (&&) or failure (||) of the command.

So we are using || which indicates we only want to perform that code if the FIND failed, meaning it couldn't find the TTL line.

http://www.robvanderwoude.com/condexec.php


»bp
0
 
Vjz1Author Commented:
ahhh ok I get it. Thanks for schooling me on this. :)
0
 
Bill PrewCommented:
My pleasure.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.