1

I'm trying to make (hopefully a simple) script that when run pings my mobile device. If the mobile is connected to the network it will execute one command and if not it will execute another.

I've tried with the following command:

ping -n 1 (The IP) >nul    
IF ERRORLEVEL 0 GOTO safe    
IF ERRORLEVEL 1 GOTO alarm

:safe    
start home.mp3    
GOTO end

:alarm    
start alarm.mp3    
GOTO end

:end
exit

This actually worked once, but since then it doesn't work after the first ping. Is it something I'm missing? Perhaps there's an easier way of doing this?

1
  • FYI - the reason this doesn't work is because the ping command does not set the errorlevel based on the result of the ping. We used to have a command line tool called "alive" that did that.
    – uSlackr
    Jun 8, 2012 at 2:30

2 Answers 2

4
ping -n 1 xxx.yyy.zzz.www | findstr TTL && start home.mp3
ping -n 1 xxx.yyy.zzz.www | findstr TTL || start alarm.mp3
3
  • This worked perfectly. Finally I can go to sleep. Thank you so much!!! Jun 7, 2012 at 23:47
  • @AntonJohnsson So this answered your question? Why did you not accept the answer?
    – 030
    Oct 18, 2014 at 15:43
  • Adding -w 1000 is a nice optimisation as this will set the timeout at 1s (1000ms)
    – Ed Mozley
    Apr 12, 2022 at 16:20
-1
ping -n 1 COMPUTERNAME | findstr TTL && start home.mp3 || start alarm.mp3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .