Last active
April 30, 2023 17:10
-
-
Save LuisPalacios/252db87b4e9866e2132e8bf8d71571cb to your computer and use it in GitHub Desktop.
Script que verifica que la configuracion de la red es correcta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# /root/firewall/firewall_verifica.sh | |
# | |
# Script que verifica que la configuracion de la red es correcta, | |
# | |
# Está relacionado con este apunte: | |
# https://www.luispa.com/administración/2023/04/08/networking-avanzado.html | |
# | |
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- | |
## Variables de trabajo | |
## | |
MIDOMINIO="tudominio.com" | |
netSetupVars="/etc/default/netSetupVars" | |
basename=`basename "$0"` | |
uso() { | |
echo "${basename}. Copyright (c) 2023 Luis Palacios" | |
exit -1 # Salimos | |
} | |
if [ ! -f ${netSetupVars} ]; then echo "Error! el fichero ${netSetupVars} no existe"; uso; fi | |
. ${netSetupVars} | |
## | |
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- | |
## Main | |
## | |
# Variables de trabajo | |
temporal=/tmp/temp-firewall-verifica.sh | |
# Usar las variables con "echo -e" para que interprete las secuencias \escaped... | |
NORMAL="\033[0;39m" | |
ROJO="\033[1;31m" | |
VERDE="\033[1;32m" | |
AMARILLO="\033[1;33m" | |
# Columna donde se muestra el mensaje de resultado (linea 1000, asi siempre | |
# se ensena en la ultima linea de la shell... y columna 80) | |
RESOK="\033[1000;80H[${VERDE}OK${NORMAL}]" | |
RESWARN="\033[1000;75H[${AMARILLO}warning${NORMAL}]" | |
RESERROR="\033[1000;77H[${ROJO}ERROR${NORMAL}]" | |
# Salir del programa | |
salir() { | |
rm -f $temporal | |
exit | |
} | |
# Funcion para comprobar el estado de las subinterfaces | |
# En $1 nos pasan el nombre de la interfaz | |
# En $2 el nivel de importancia que tiene (1-aviso, 2 o cualquier | |
# otro numero entonces es critico) si la interfaz no esta disponible | |
test_intf() { | |
# Get the IP Address from first argument | |
intf=$1 | |
nivel=$2 | |
echo -n "Probando interface: $intf" | |
ip link show $intf 2>/dev/null | grep UP > /dev/null 2>/dev/null | |
ret=$? | |
if [ "$ret" = "0" ] ; then | |
echo -e "${RESOK}" | |
else | |
if [ "$nivel" = "1" ]; then | |
echo -e "${RESWARN}" | |
else | |
echo -e "${RESERROR}" | |
fi | |
fi | |
} | |
# Funcion para comprobar el estado de un parámetro del Kernel | |
# En $1 nos pasan el valor que debería obtenerse para considerarse como bueno | |
# En $2 el nombre del fichero (en realidad es el parámetro del kernel) | |
test_kernel() { | |
# Get the file name to check and the desired value | |
wanted=$1 | |
kfilename=$2 | |
echo -n "Comprobando Kernel: ${kfilename}" | |
ret=`cat ${kfilename} 2>/dev/null` | |
if [ "${ret}" = "${wanted}" ] ; then | |
echo -e " (OK=${wanted}) ${RESOK}" | |
else | |
echo -e " (OK=${wanted}) ${RESERROR}" | |
fi | |
} | |
# Funcion para probar conectividad con la direccion | |
# IP que nos pasan en '$1' y mostrando detras el mensajje en '$2' | |
# En $3 el nivel de importancia que tiene (1-aviso, 2 o cualquier | |
# otro critico) si la interfaz no esta disponible | |
test_ip() { | |
# Get the IP Address from first argument | |
ip=$1 | |
txt=$2 | |
nivel=$3 | |
echo -n "Probando IP $ip: $txt ..." | |
ping -c 1 $ip > $temporal 2>/dev/null | |
ret=$? | |
if [ "$ret" = "0" ] ; then | |
ms=`cat $temporal | grep rtt | awk 'BEGIN{FS="/"};{printf $5}'` | |
echo -e "${RESOK} $ms ms" | |
else | |
if [ "$nivel" = "1" ]; then | |
echo -e "${RESWARN}" | |
else | |
echo -e "${RESERROR}" | |
fi | |
fi | |
} | |
# Funcion que prueba acceso a internet y en caso contrario sale | |
salir_si_no_hay_internet() { | |
ip="8.8.8.8" | |
echo -n "INTERNET ($ip)" | |
ping -c 1 $ip > $temporal 2>/dev/null | |
ret=$? | |
if [ "$ret" = "0" ] ; then | |
ms=`cat $temporal | grep rtt | awk 'BEGIN{FS="/"};{printf $5}'` | |
echo -e "${RESOK} $ms ms" | |
else | |
echo -e "${RESERROR}" | |
echo | |
exit | |
fi | |
} | |
# Commprobar que 'registro' de mi dns server externo funciona bien | |
test_dns() { | |
# get parameters | |
ip=$1 | |
echo -n "Comprobando: DNS Server ${ip} UDP 53" | |
ret=`dig +time=1 +tries=1 ${MIDOMINIO} @${ip}|grep -i SOA` | |
if [ "${?}" = "0" ] ; then | |
echo -e "${RESOK}" | |
else | |
echo -e "${RESERROR}" | |
fi | |
} | |
# Comprobar conectividad con la direccion IPv6 en '$1', mostrando mensaje '$2'. | |
# En $2 el nivel de importancia que tiene (1-aviso, 2 o cualquier | |
# otro numero entonces es critico) si la interfaz no esta disponible | |
test_unit() { | |
unit=$1 | |
txt=$2 | |
nivel=$3 | |
echo -n "Unit (servicio) ${unit}: $txt ..." | |
systemctl is-active ${unit} > ${temporal} 2>/dev/null | |
ret=$? | |
if [ "$ret" = "0" ] ; then | |
echo -e "${RESOK}" | |
else | |
if [ "$nivel" = "1" ]; then | |
echo -e "${RESWARN}" | |
else | |
echo -e "${RESERROR}" | |
fi | |
fi | |
} | |
# | |
# Verificar la conectividad en IP | |
# =============================== | |
test_unit firewall_1_pre_network.service "IPTABLES (pre-net)" | |
test_unit firewall_2_post_network.service "IPTABLES (post-net)" | |
test_unit [email protected] "PPP Movistar" | |
salir_si_no_hay_internet | |
test_unit sshd.service "SSHD" | |
test_unit snmpd.service "Servicio SNMP" | |
test_unit chrony.service "Servicio Horario NTP" | |
# | |
# Verificar la conectividad en IP | |
# =============================== | |
test_intf lo 2 | |
test_intf ${ifLAN} 2 | |
test_intf ${ifWAN} 2 | |
test_intf ${ifMovistarDatos} 2 | |
# | |
# Verificar la conectividad en IP | |
# =============================== | |
test_ip localhost "localhost (lo)" | |
test_ip 192.168.100.224 "PiHOLE" | |
# Compruebo el forwarding | |
test_kernel 1 /proc/sys/net/ipv4/ip_forward | |
test_kernel 1 /proc/sys/net/ipv4/conf/default/rp_filter | |
test_kernel 0 /proc/sys/net/ipv4/conf/lo/rp_filter | |
test_kernel 1 /proc/sys/net/ipv4/conf/${ifLAN}/rp_filter | |
test_kernel 1 /proc/sys/net/ipv4/conf/${ifWAN}/rp_filter | |
test_kernel 1 /proc/sys/net/ipv4/conf/${ifMovistarDatos}/rp_filter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment