Notify me when the site (server) is online again

When I ping one site, it returns "Request timeout." I want to make a small program that tells me (a beep or something like that) when this server reconnects to the network. No matter what language. I think this should be a very simple script with a few lines of code. So how to write this?

+4
source share
3 answers

Do not forget the beep, for example echo "^ G"! Just being different - here's the Windows package:

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 

:)

+1
source

Some ping implementations allow you to specify conditions for exiting after receiving packets:

On Mac OS X, use ping -a -o $the_host

  • ping will try (default)
  • -a means a sound signal when receiving a packet
  • -o means exit when receiving a packet

On Linux (at least Ubuntu) use ping -a -c 1 -w inf $the_host

  • -a means a sound signal when receiving a packet
  • -c 1 indicates the number of packets to send before exiting (in this case 1)
  • -w inf indicates the deadline when ping exits no matter what (in this case, Infinite)
  • when -c and -w are used together, -c becomes the number of packets received before exiting

Or you can bind the chain to execute the next command, for example. on ssh to the server as soon as it appears (with a gap between allowing sshd actually run):

 # ping -a -o $the_host && sleep 3 && ssh $the_host 
+3
source

One way to do ping is to loop, for example

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

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

On some systems, such as Mac OS X, ping , the -a -o options may also be available (according to another answer), which will cause it to continue to ping until it receives a response. However, ping for many (most?) Linux systems does not have the -o option, and the equivalent type -c 1 -w 0 will still exit if the network returns an error.

Edit: if the host does not respond to ping or you need to check the availability of the service on a specific port, you can use netcat in zero input / output mode:

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

-w 5 sets a 5 second timeout for each individual attempt. Note that with netcat, you can even list several ports (or port ranges) for scanning when some of them become available.

Edit 2: The loops shown above continue to try until the host (or port) is reached. Add your alert command after them, for example. beep or popup.

0
source

Source: https://habr.com/ru/post/1306591/


All Articles