Skip to content

Instantly share code, notes, and snippets.

@LuisPalacios
Last active March 8, 2023 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuisPalacios/eee992475e67e3425a73720d43df1f4d to your computer and use it in GitHub Desktop.
Save LuisPalacios/eee992475e67e3425a73720d43df1f4d to your computer and use it in GitHub Desktop.
Sur: Comprobar conectividad
#!/bin/bash
#
##
## /root/firewall/sur_verifica_conectividad.sh
##
## Script que configura iptables en un servidor Raspbian OS
##
## Este fichero está relacionado con este apunte:
## https://www.luispa.com/linux/2014/10/19/bridge-ethernet.html
##
##
# Script que verifica que la configuracion de la red es correcta,
# que hay conectividad con internet, prueba servicios, etc.
#
# Copyright (C) 2006 Luis Palacios
#
# Variables de trabajo
temporal=/tmp/temp-parchis-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}`
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 para probar conectividad con la direccion
# IP que nos pasan desde '$1' a '$2'. Muestra el mensaje de '$3'.
# En $4 el nivel de importancia que tiene (1-aviso, 2 o cualquier
# otro critico) si la interfaz no esta disponible
test_ip_src_dst() {
# Get the IP Address from first argument
ip_src=$1
ip_dst=$2
txt=$3
nivel=$4
echo -n "Probando IP $ip_dst: $txt ..."
ping -c 1 -I $ip_src $ip_dst > $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
}
# Commprobar que 'registro' nuestro dns server funciona bien
test_dns() {
# get parameters
ip=$1
echo -n "Comprobando: DNS Server ${ip} UDP 53"
ret=`dig +time=1 +tries=1 parchis.org @${ip}|grep -i SOA`
if [ "${?}" = "0" ] ; then
echo -e "${RESOK}"
else
echo -e "${RESERROR}"
fi
}
# Funcion para probar que el DNS Server funciona bien, consulto el SOA
test_dns_soa() {
# Get the domain name
domainname=$1
txt=$2
echo -n "Comprobando: ${txt}"
ret=`dig +time=1 @172.26.23.3 ${domainname} | grep 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 el forwarding y RPF en el Kernel
# =========================================
# Cargo los nombres de las interfaces
. /root/firewall/sur_firewall_inames.sh
#
# Verificar la conectividad en IP
# ===============================
echo
test_unit firewall_1_pre_network "IPTABLES (pre-net)"
test_unit firewall_2_post_network "IPTABLES (post-net)"
test_unit watch_eth_bridge_con_norte.timer "Vigilante"
test_unit sshd.service "SSHD"
#
# Verificar la conectividad en IP
# ===============================
test_intf lo 2
test_intf ${ifWan} 2
test_intf ${ifBridgeIPTV} 2
test_intf ${ifLanInternetViaNorte} 2
test_intf ${ifLanInternetViaSur} 2
test_intf ${ifTunelNorte} 2
#
# Verificar la conectividad en IP
# ===============================
test_ip localhost "localhost (lo)"
test_ip 192.168.1.1 "Router de acceso a Internet"
test_ip 192.168.107.254 "Switch local"
test_ip 192.168.224.1 "Tunel Internet con Norte"
test_ip 192.168.206.1 "Tunel IPTV con Norte"
test_ip_src_dst 192.168.206.2 10.64.0.1 "MOVISTAR IPTV"
#test_ip 192.168.10.XX "PONER AQUI UN EQUIPO EN LA LAN"
# Compruebo el forwarding
test_kernel 1 /proc/sys/net/ipv4/ip_forward
# Compruebo RPF's
test_kernel 0 /proc/sys/net/ipv4/conf/all/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/default/rp_filter
test_kernel 0 /proc/sys/net/ipv4/conf/${ifBridgeIPTV}/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/${ifLanInternetViaNorte}/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/${ifLanInternetViaSur}/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/${ifTunelNorte}/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/${ifTunelNorte}/forwarding
test_kernel 0 /proc/sys/net/ipv4/conf/lo/rp_filter
test_kernel 1 /proc/sys/net/ipv4/conf/${ifWan}/rp_filter
test_kernel 0 /proc/sys/net/ipv4/conf/lo/rp_filter
echo "WAN Interfaz Internet : ${ifWan} - ${ipWan}"
echo "Bridge IPTV : ${ifBridgeIPTV} - ${ipBridgeIPTV}"
echo "LAN Acceso Internet Via Norte : ${ifLanInternetViaNorte} - ${ipLanInternetViaNorte}"
echo "LAN Acceso Internet Via Sur : ${ifLanInternetViaSur} - ${ipLanInternetViaSur}"
echo "INTRANET : ${INTRANET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment