Script Bash · 3 min read · Jan 20, 2026

Script Bash Simple Pour Surveiller Votre Serveur Web À Distance Sur Différents Ports

Script Bash Simple Pour Surveiller Votre Serveur Web À Distance Sur Différents Ports

Script bash simple pour surveiller un serveur web sur différents ports (ici smtp, dns, http et https mais cela peut être personnalisé) ; je suis sûr qu’il existe plus de 100 programmes disponibles pour cela, mais je voulais quelque chose avec une faible utilisation de mémoire. De plus, je voulais seulement être notifié une fois, les notifications sont reçues par SMS sur mon téléphone. Avec le logiciel que j’utilisais auparavant, j’étais notifié chaque minute jusqu’à ce que je puisse atteindre un ordinateur et résoudre le problème ou arrêter la surveillance, ce qui était assez ennuyeux.

Installation du logiciel

Vous devez avoir mail, dig et telnet installés.

Le script

NOTE : Le script ne continuera pas tant que vous ne faites pas :

./whatever_you_called_this_script fix

C’est fait exprès pour recevoir seulement UNE notification…

#!/bin/bash
# Script pour vérifier les ports importants sur un serveur web à distance
# Copyright (c) 2009 blogama.org
# Ce script est sous licence GNU GPL version 2.0 ou supérieure
# ---------------------------------------------------------------------
 
### Ce script effectue une vérification sur les ports 25, 53, 80 et 443 ###
### Après 2 vérifications échouées, il enverra une notification par mail ###
 
######À modifier######
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"
#########
###NOTIFICATIONS###
EMAIL="[email protected]"
##########
######Fin à modifier######
 
######Ne faites pas de modifications ci-dessous######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Changer de répertoire###
cd $WORKDIR
###Restaurer lorsque le problème est résolu###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
    exit 1;
  fi
fi
###Vérifier si déjà notifié###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Tester SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_smtp.txt ]; then #supprimer le fichier si le problème est résolu
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Échec de la connexion
  if [ -f server_problem_first_time_smtp.txt ]; then #Deuxième fois, envoyer la notification ci-dessous
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_smtp.txt
  else #Première notification
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Tester HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_http.txt ]; then #supprimer le fichier si le problème est résolu
    rm -rf server_problem_first_time_http.txt
  fi
else #Échec de la connexion
  if [ -f server_problem_first_time_http.txt ]; then #Deuxième fois, envoyer la notification ci-dessous
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_http.txt
  else #Première notification
    > server_problem_first_time_http.txt
  fi
fi
 
###Tester HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_https.txt ]; then #supprimer le fichier si le problème est résolu
    rm -rf server_problem_first_time_https.txt
  fi
else #Échec de la connexion
  if [ -f server_problem_first_time_https.txt ]; then #Deuxième fois, envoyer la notification ci-dessous
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_https.txt
  else #Première notification
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Tester DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #supprimer le fichier si le problème est résolu
    rm -rf server_problem_first_time_dns.txt
  fi
else #Échec de la connexion
  if [ -f server_problem_first_time_dns.txt ]; then #Deuxième fois, envoyer la notification ci-dessous
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_dns.txt
  else #Première notification
    > server_problem_first_time_dns.txt
  fi
fi
 
###Envoyer une notification par mail après 2 vérifications échouées###
if [ -f server_problem.txt ]; then
  $MAIL -s "Problème de serveur" $EMAIL < /root/server_problem.txt
fi

Rendez-le exécutable :

chmod +x whatever_you_called_this_script

Ajoutez-le à votre crontab :

crontab -e
* * * * * /path/to/whatever_you_called_this_script >/dev/null 2>&1
Share: X/Twitter LinkedIn

Recevez de nouveaux articles dans votre boîte de réception.

Aucun spam. Désabonnez-vous à tout moment.