Script Bash · 3 min read · Jan 20, 2026

Semplice script Bash per monitorare il tuo server web da remoto su porte diverse

Semplice script Bash per monitorare il tuo server web da remoto su porte diverse

Semplice script bash per monitorare un server web su porte diverse (qui smtp, dns, http e https ma può essere personalizzato); sono sicuro che ci sono oltre 100 programmi disponibili che fanno questo, ma volevo qualcosa con un basso utilizzo di memoria. Inoltre, volevo essere avvisato solo una volta, le notifiche vengono ricevute via SMS sul mio cellulare. Con il software che stavo usando prima, ricevevo notifiche ogni minuto fino a quando non riuscivo a raggiungere un computer e risolvere il problema o smettere di monitorare, il che era piuttosto fastidioso.

Installazione del software

Hai bisogno di mail, dig e telnet installati.

Lo script

NOTA: Lo script non continuerà finché non fai:

./whatever_you_called_this_script fix

Questo è fatto apposta per ricevere solo UNA notifica…

#!/bin/bash
# Script per controllare porte importanti su server web remoto
# Copyright (c) 2009 blogama.org
# Questo script è concesso in licenza sotto GNU GPL versione 2.0 o superiore
# ---------------------------------------------------------------------
 
### Questo script verifica le porte 25, 53, 80 e 443 ###
### Dopo 2 controlli falliti invierà una notifica via mail ###
 
######Da modificare######
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"
#########
###NOTIFICHE###
EMAIL="[email protected]"
##########
######Fine da modificare######
 
######Non apportare modifiche sotto######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Cambia dir###
cd $WORKDIR
###Ripristina quando il problema è risolto###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
    exit 1;
  fi
fi
###Controlla se già notificato###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Test 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 #rimuovi file se il problema è risolto
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Fallimento connessione
  if [ -f server_problem_first_time_smtp.txt ]; then #Seconda volta, invia notifica sotto
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_smtp.txt
  else #Prima notifica
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Test 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 #rimuovi file se il problema è risolto
    rm -rf server_problem_first_time_http.txt
  fi
else #Fallimento connessione
  if [ -f server_problem_first_time_http.txt ]; then #Seconda volta, invia notifica sotto
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_http.txt
  else #Prima notifica
    > server_problem_first_time_http.txt
  fi
fi
 
###Test 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 #rimuovi file se il problema è risolto
    rm -rf server_problem_first_time_https.txt
  fi
else #Fallimento connessione
  if [ -f server_problem_first_time_https.txt ]; then #Seconda volta, invia notifica sotto
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_https.txt
  else #Prima notifica
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Test DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #rimuovi file se il problema è risolto
    rm -rf server_problem_first_time_dns.txt
  fi
else #Fallimento connessione
  if [ -f server_problem_first_time_dns.txt ]; then #Seconda volta, invia notifica sotto
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
    rm -rf server_problem_first_time_dns.txt
  else #Prima notifica
    > server_problem_first_time_dns.txt
  fi
fi
 
###Invia notifica via mail dopo 2 controlli falliti###
if [ -f server_problem.txt ]; then
  $MAIL -s "Problema server" $EMAIL < /root/server_problem.txt
fi

Rendilo eseguibile:

chmod +x whatever_you_called_this_script

Aggiungilo al tuo crontab:

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

Ricevi i nuovi post nella tua casella di posta.

Nessuno spam. Disiscriviti in qualsiasi momento.