views:

76

answers:

3

When I ping one site it returns "Request timed out". I want to make little program that will inform me (sound beep or something like that) when this server is online again. No matter in which language. I think it should be very simple script with a several lines of code. So how to write it?

+3  A: 

Use ping -a -o $the_host

  • ping will keep trying
  • -a means beep when a packet is received
  • -o means exit when a packet is received
Stephen
Is `-o` a standard option? I don't see it on Linux or Windows
Michael Mrozek
Ah, my bad. It seems to only be avail on mac. I guess he can wrap it in a loop.
Stephen
Confirm that `-o` is not available on Linux. Too bad, would be very handy for this use. +1 anyhow for showing me the option on my Mac. =)
Arkku
A: 

One way is to run ping is a loop, e.g.

while ! ping -c 1 host; do sleep 1; done

(You can redirect the output to /dev/null if you want to keep it quiet.)

On some systems, such as Mac OS X, ping may also have the options -a -o (as per another answer) available which will cause it to keep pinging until a response is received. However, the ping on many (most?) Linux systems does not have the -o option and the kind of equivalent -c 1 -w 0 still exits if the network returns an error.

Edit: If the host does not respond to ping or you need to check the availability of service on a certain port, you can use netcat in the zero I/O mode:

while ! nc -w 5 -z host port; do sleep 1; done

The -w 5 specifies a 5 second timeout for each individual attempt. Note that with netcat you can even list multiple ports (or port ranges) to scan when some of them becomes available.

Edit 2: The loops shown above keep trying until the host (or port) is reached. Add your alert command after them, e.g. beep or pop-up a window.

Arkku
+1  A: 

Don't forget the notify sound like echo"^G"! Just to be different - here's Windows batch:

C:\> more pingnotify.bat
:AGAIN
ping -n 1 %1%
IF ERRORLEVEL 1 GOTO AGAIN
sndrec32 /play /close "C:\Windows\Media\Notify.wav"

C:\> pingnotify.bat localhost

:)

Bert F