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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vjz1Author Commented:
That works. Thanks.
0
Vjz1Author Commented:
Thanks.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.