I want to continuously ping a server and see a message box when ever it responds i.e. server is currently down. I want to do it through batch file.
I can show a message box as said here Show a popup/message box from a Windows batch file
and can ping continuously by
ping <servername> -t
But how do I check if it responded or not?
I've seen three results to a ping - The one we "want" where the IP replies, "Host Unreachable" and "timed out" (not sure of exact wording).
The first two return ERRORLEVEL of 0.
Timeout returns ERRORLEVEL of 1.
Are the other results and error levels that might be returned? (Besides using an invalid switch which returns the allowable switches and an errorlevel of 1.)
Apparently Host Unreachable can use one of the previously posted methods (although it's hard to figure out when someone replies which case they're writing code for) but does the timeout get returned in a similar manner that it can be parsed?
In general, how does one know what part of the results of the ping can be parsed? (Ie, why might Sent and/or Received and/or TTL be parseable, but not host unreachable?
Oh, and iSid, maybe there aren't many upvotes because the people that read this don't have enough points. So they get their question answered (or not) and leave.
I wasn't posting the above as an answer. It should have been a comment but I didn't see that choice.
I have made a variant solution based on paxdiablo's post
Place the following code in Waitlink.cmd
For example use it from another batch file like this
The call to waitlink will only return when a ping was succesful. Thanks to paxdiablo and Gabe. Hope this helps someone else.
The question was to see if ping responded which this script does.
However this will not work if you get the Host Unreachable message as this returns ERRORLEVEL 0 and passes the check for Received = 1 used in this script, returning Link is UP from the script. Host Unreachable occurs when ping was delivered to target notwork but remote host cannot be found.
If I recall the correct way to check if ping was successful is to look for the string 'TTL' using Find.
This wont work with IPv6 networks because ping will not list TTL when receiving reply from IPv6 address.
You can ping without "-t" and check the exit code of the ping. It reports failure when there is no answer.
Simple version:
But sometimes first ping just fail and second one work (or vice versa) right? So we want to get success when at least one ICMP reply has been returned successfully:
I hope this helps someone. I use this bit of logic to verify if network shares are responsive before checking the individual paths. It should handle DNS names and IP addresses
A valid path in the text file would be \192.168.1.2\'folder' or \NAS\'folder'