Bash : script para comprobar la conexión con un servidor usando sonido

El siguiente código escrito en bash comprueba la conexión con un servidor. Si dicho servidor está vivo ( is alive!! ) no realiza ninguna salida. Si el servidor no responde a los pings (está caido) hará que suene un archivo de sonido (alarma).

Es útil para dejarlo en segundo plano y avisarnos si una maquina se ha caido. O bien para comprobar periódicamente si estamos conectados a internet (por ejemplo poniendo un host como www.google.es) de modo que si perdemos nuestra conexión local sonará la alarma.

Tiene dos tiempos configurables que indican que tiempo hay que esperar entre comprobación y comprobación.

#/bin/bash
#
# Copyright (C) 2010 dieguz2
# License: GNU GPL v3
# http://dieguz2.blogspot.com/
# Contributors: flipa_illo
# This script check if the hostname passed as argument responds ping probes. If host is down then plays a wav file.
# name: isalive.sh
# Usage: isalive.sh hostname
# Usage example: ./isalive.sh www.google.es

#Variables
sleeptime_all_ok=60 # time between ping probes when target host is alive
sleeptime_hostdown=180 # time between ping probes when target host is down

wavefile=/home/dieguz/Música/beep.wav

while true; do
comando=`ping -c 1 $1 2> /dev/null`
nofunciona=$?
#0 host exists and is alive
#1 host exists and is not alive
#2 host doesnt exists
if [ $nofunciona -eq 1 ] ; then
/usr/bin/playwave $wavefile &> /dev/null
sleeptime=$sleeptime_hostdown
else
if [ $nofunciona -eq 2 ] ; then
echo "unknown host"
exit 2;
else
sleeptime=$sleeptime_all_ok

fi

fi
sleep $sleeptime
done

Comentarios

Entradas populares de este blog

Compilar php 4.4 con ldap en centOs

Oracle : Tablas y objetos bloqueados