Script Bash · 3 min read · Jan 20, 2026

Script Bash Simples Para Monitorar Seu Servidor Web Remotamente Em Diferentes Portas

Script Bash Simples Para Monitorar Seu Servidor Web Remotamente Em Diferentes Portas

Script bash simples para monitorar um servidor web em diferentes portas (aqui smtp, dns, http e https, mas pode ser personalizado); tenho certeza de que existem mais de 100 programas disponíveis fazendo isso, mas eu queria algo com baixo uso de memória. Além disso, eu só queria ser notificado uma vez, as notificações são recebidas por SMS no meu celular. Com o software que eu estava usando antes, eu recebia notificações a cada minuto até que eu pudesse alcançar um computador e corrigir o problema ou parar a monitoração, o que era bastante irritante.

Instalação do software

Você precisa ter mail, dig e telnet instalados.

O script

NOTA: O script não continuará até que você faça:

./whatever_you_called_this_script fix

Isso é feito de propósito para receber apenas UMA notificação…

#!/bin/bash
# Script para verificar portas importantes em um servidor web remoto
# Copyright (c) 2009 blogama.org
# Este script é licenciado sob a GNU GPL versão 2.0 ou superior
# ---------------------------------------------------------------------
 
### Este script faz uma verificação nas portas 25, 53, 80 e 443 ###
### Após 2 verificações falhadas, ele enviará uma notificação por e-mail ###
 
######A 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"
#########
###NOTIFICAÇÕES###
EMAIL="[email protected]"
##########
######Fim a ser modificado######
 
######Não faça modificações abaixo######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Mudar dir###
cd $WORKDIR
###Restaurar quando o problema for corrigido###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
    exit 1;
  fi
fi
###Verificar se já foi notificado###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Testar SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORTA CONECTADA"
  if [ -f server_problem_first_time_smtp.txt ]; then #remover arquivo se o problema for corrigido
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Falha de conexão
  if [ -f server_problem_first_time_smtp.txt ]; then #Segunda vez, enviar notificação abaixo
    echo "PORTA SMTP NÃO CONECTANDO" >> server_problem.txt
    rm -rf server_problem_first_time_smtp.txt
  else #Primeira notificação
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Testar HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORTA CONECTADA"
  if [ -f server_problem_first_time_http.txt ]; then #remover arquivo se o problema for corrigido
    rm -rf server_problem_first_time_http.txt
  fi
else #Falha de conexão
  if [ -f server_problem_first_time_http.txt ]; then #Segunda vez, enviar notificação abaixo
    echo "PORTA HTTP NÃO CONECTANDO" >> server_problem.txt
    rm -rf server_problem_first_time_http.txt
  else #Primeira notificação
    > server_problem_first_time_http.txt
  fi
fi
 
###Testar HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORTA CONECTADA"
  if [ -f server_problem_first_time_https.txt ]; then #remover arquivo se o problema for corrigido
    rm -rf server_problem_first_time_https.txt
  fi
else #Falha de conexão
  if [ -f server_problem_first_time_https.txt ]; then #Segunda vez, enviar notificação abaixo
    echo "PORTA HTTPS NÃO CONECTANDO" >> server_problem.txt
    rm -rf server_problem_first_time_https.txt
  else #Primeira notificação
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Testar DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORTA CONECTADA"
  if [ -f server_problem_first_time_dns.txt ]; then #remover arquivo se o problema for corrigido
    rm -rf server_problem_first_time_dns.txt
  fi
else #Falha de conexão
  if [ -f server_problem_first_time_dns.txt ]; then #Segunda vez, enviar notificação abaixo
    echo "PORTA DNS NÃO CONECTANDO" >> server_problem.txt
    rm -rf server_problem_first_time_dns.txt
  else #Primeira notificação
    > server_problem_first_time_dns.txt
  fi
fi
 
###Enviar notificação por e-mail após 2 verificações falhadas###
if [ -f server_problem.txt ]; then
  $MAIL -s "Problema no servidor" $EMAIL < /root/server_problem.txt
fi

Torne-o executável:

chmod +x whatever_you_called_this_script

Adicione-o ao seu crontab:

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

Receba novas postagens na sua caixa de entrada

Sem spam. Cancele a assinatura a qualquer momento.