Bash Script · 3 min read · Jan 20, 2026
Script Bash Simple Para Monitorear Tu Servidor Web Remotamente En Diferentes Puertos
Script Bash Simple Para Monitorear Tu Servidor Web Remotamente En Diferentes Puertos
Script bash simple para monitorear un servidor web en diferentes puertos (aquí smtp, dns, http y https, pero se puede personalizar); estoy seguro de que hay más de 100 programas disponibles que hacen esto, pero quería algo con bajo uso de memoria. Además, solo quería ser notificado una vez, las notificaciones se reciben por SMS en mi celular. Con el software que estaba usando antes, recibía notificaciones cada minuto hasta que podía llegar a una computadora y solucionar el problema o detener el monitoreo, lo cual era bastante molesto.
Instalación del software
Necesitas tener mail, dig y telnet instalados.
El script
NOTA: El script no continuará hasta que hagas:
./whatever_you_called_this_script fixEsto se hace a propósito para recibir solo UNA notificación…
#!/bin/bash
# Script para verificar puertos importantes en un servidor web remoto
# Copyright (c) 2009 blogama.org
# Este script está licenciado bajo GNU GPL versión 2.0 o superior
# ---------------------------------------------------------------------
### Este script verifica los puertos 25, 53, 80 y 443 ###
### Después de 2 verificaciones fallidas, enviará una notificación por correo ###
######Para ser modificado######
WORKDIR="/root"
###HTTP###
HTTPSERVERIP="192.168.1.106"
HTTPSERVERPORT="80"
##########
###HTTPS###
HTTPSSERVERIP="192.168.1.106"
HTTPSSERVERPORT="443"
##########
###MAIL###
SMTPSERVERIP="192.168.1.106"
SMTPSERVERPORT="25"
##########
###DNS###
DNSSERVERIP="192.168.1.106"
DOMAINTOCHECKDNS="example.com"
ANSWERIP="192.168.1.106"
#########
###NOTIFICACIONES###
EMAIL="[email protected]"
##########
######Fin de ser modificado######
######No hagas modificaciones debajo######
### Binarios ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Cambiar dir###
cd $WORKDIR
###Restaurar cuando se solucione el problema###
if [ $1 ]; then
if [ $1=="fix" ]; then
rm server_problem*.txt
exit 1;
fi
fi
###Verificar si ya se notificó###
if [ -f server_problem.txt ]; then
exit 1;
fi
###Probar SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PUERTO CONECTADO"
if [ -f server_problem_first_time_smtp.txt ]; then #eliminar archivo si el problema se solucionó
rm -rf server_problem_first_time_smtp.txt
fi
else #Fallo de conexión
if [ -f server_problem_first_time_smtp.txt ]; then #Segunda vez, enviar notificación abajo
echo "PUERTO SMTP NO CONECTANDO" >> server_problem.txt
rm -rf server_problem_first_time_smtp.txt
else #Primera notificación
> server_problem_first_time_smtp.txt
fi
fi
###Probar HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PUERTO CONECTADO"
if [ -f server_problem_first_time_http.txt ]; then #eliminar archivo si el problema se solucionó
rm -rf server_problem_first_time_http.txt
fi
else #Fallo de conexión
if [ -f server_problem_first_time_http.txt ]; then #Segunda vez, enviar notificación abajo
echo "PUERTO HTTP NO CONECTANDO" >> server_problem.txt
rm -rf server_problem_first_time_http.txt
else #Primera notificación
> server_problem_first_time_http.txt
fi
fi
###Probar HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
echo "PUERTO CONECTADO"
if [ -f server_problem_first_time_https.txt ]; then #eliminar archivo si el problema se solucionó
rm -rf server_problem_first_time_https.txt
fi
else #Fallo de conexión
if [ -f server_problem_first_time_https.txt ]; then #Segunda vez, enviar notificación abajo
echo "PUERTO HTTPS NO CONECTANDO" >> server_problem.txt
rm -rf server_problem_first_time_https.txt
else #Primera notificación
> server_problem_first_time_https.txt
fi
fi
###Probar DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
if [ "$?" -ne "1" ]; then #Ok
echo "PUERTO CONECTADO"
if [ -f server_problem_first_time_dns.txt ]; then #eliminar archivo si el problema se solucionó
rm -rf server_problem_first_time_dns.txt
fi
else #Fallo de conexión
if [ -f server_problem_first_time_dns.txt ]; then #Segunda vez, enviar notificación abajo
echo "PUERTO DNS NO CONECTANDO" >> server_problem.txt
rm -rf server_problem_first_time_dns.txt
else #Primera notificación
> server_problem_first_time_dns.txt
fi
fi
###Enviar notificación por correo después de 2 verificaciones fallidas###
if [ -f server_problem.txt ]; then
$MAIL -s "Problema del servidor" $EMAIL < /root/server_problem.txt
fi
Hazlo ejecutable:
chmod +x whatever_you_called_this_scriptAgrégalo a tu crontab:
crontab -e* * * * * /path/to/whatever_you_called_this_script >/dev/null 2>&1Recibe nuevas publicaciones en tu bandeja de entrada.
No spam. Cancela la suscripción en cualquier momento.