Add ping_loop.sh

This commit is contained in:
2025-10-21 23:39:37 +02:00
parent 9e0b80ab12
commit 9aa309205e

59
01_bash/ping_loop.sh Normal file
View File

@@ -0,0 +1,59 @@
#!/bin/bash
# ======================= #
# 🎯 Colores personalizados
# ======================= #
RED='\033[1;91m'
GREEN='\033[1;92m'
CYAN='\033[1;96m'
YELLOW='\033[1;93m'
MAGENTA='\033[1;95m'
BOLD='\033[1m'
NC='\033[0m' # Sin color
# ======================= #
# 👋 Función al salir
# ======================= #
function despedida() {
echo -e "\n${MAGENTA}${BOLD}👋 Gracias por usar el script. ¡Hasta luego!${NC}"
exit 0
}
# Capturar Ctrl + C
trap despedida SIGINT
# ======================= #
# 🖥️ Título del script
# ======================= #
echo -e "${CYAN}${BOLD}=============================="
echo -e " PING LOOP SCRIPT"
echo -e "==============================${NC}"
# ======================= #
# 🔧 Solicitar parámetros
# ======================= #
read -p "$(echo -e "${YELLOW}${BOLD}🌐 Introduce la web o IP a hacer ping: ${NC}")" destino
read -p "$(echo -e "${YELLOW}${BOLD}⏱️ Intervalo entre intentos fallidos (en segundos): ${NC}")" intervalo
# ======================= #
# 🔁 Bucle de ping
# ======================= #
contador=0
while true; do
((contador++))
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
salida_ping=$(ping -c1 -W1 -w5 "$destino" 2>&1)
if echo "$salida_ping" | grep -q 'time='; then
rtt=$(echo "$salida_ping" | sed -n 's/.*time=\([0-9.]* ms\).*/\1/p')
echo -e "${GREEN}${BOLD}[✓] Intento $contador - $timestamp - Respuesta de $destino: $rtt${NC}"
break
else
echo -e "${RED}${BOLD}[✗] Intento $contador - $timestamp - Sin respuesta de $destino${NC}"
sleep "$intervalo"
fi
done
# Mensaje final (en caso de salir por éxito)
despedida