From a02991c15056db1d80325ae9244b6181cb2fa42b Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Wed, 7 Feb 2024 00:13:40 +0100 Subject: [PATCH] Update pruebaRed.sh --- src/pruebaRed.sh | 166 +++++++++++++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 55 deletions(-) diff --git a/src/pruebaRed.sh b/src/pruebaRed.sh index ed160e8..11629cc 100755 --- a/src/pruebaRed.sh +++ b/src/pruebaRed.sh @@ -62,9 +62,9 @@ print_network_table() { # Comprobación de dependencias check_dependencies() { - command -v jq >/dev/null 2>&1 || { echo >&2 "${COLOR_RED}jq no está instalado. Instálalo con 'sudo apt-get install jq'${COLOR_RESET}"; exit 1; } - command -v nordvpn >/dev/null 2>&1 || { echo >&2 "${COLOR_RED}nordvpn no está instalado. Instálalo con 'sudo apt-get install nordvpn'${COLOR_RESET}"; exit 1; } - command -v speedtest >/dev/null 2>&1 || { echo >&2 "${COLOR_RED}speedtest no está instalado. Instálalo con 'sudo apt-get install speedtest-cli'${COLOR_RESET}"; exit 1; } + for cmd in "jq" "nordvpn" "speedtest"; do + command -v "$cmd" >/dev/null 2>&1 || { echo >&2 "${COLOR_RED}$cmd no está instalado. Instálalo con 'sudo apt-get install $cmd'${COLOR_RESET}"; exit 1; } + done } # Función para obtener la IP del router @@ -77,67 +77,123 @@ get_router_ip() { network_info=() # Comprobar conexión -print_title "COMPROBAR CONEXIÓN" -if ping -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then - echo -e "\t${COLOR_GREEN}Conexión exitosa con 8.8.8.8${COLOR_RESET}" -else - echo -e "\t${COLOR_RED}Error de conexión con 8.8.8.8${COLOR_RESET}" - - # Obtener la interfaz predeterminada - default_interface=$(ip route show default | awk '/default/ {print $5}') - - # Probar la conexión con el router - router_ip=$(get_router_ip "$default_interface") - if ping -c 1 -W 2 "$router_ip" > /dev/null 2>&1; then - echo -e "\t${COLOR_GREEN}Conexión exitosa con el router ($router_ip)${COLOR_RESET}" +check_connection() { + print_title "COMPROBAR CONEXIÓN" + if ping -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then + echo -e "\t${COLOR_GREEN}Conexión exitosa con 8.8.8.8${COLOR_RESET}" else - echo -e "\t${COLOR_RED}Error de conexión con el router ($router_ip)${COLOR_RESET}" - exit 1 # Agregar la instrucción exit para detener el script - fi -fi - - -# Lista de sitios web a comprobar -websites=("vergaracarmona.es" "gitea.vergaracarmona.es" "diariosenderista.es" "marialuisasefardi.es" "prefapp.es") - -# Verificar el estado de sitios web -print_title "VERIFICANDO ESTADO DE WEBS" -check_website_status() { - local website=$1 - if ping -c 1 -W 2 "$website" > /dev/null 2>&1; then - echo -e "\t${COLOR_GREEN}$website está levantado${COLOR_RESET}" - else - echo -e "\t${COLOR_RED}$website está caído${COLOR_RESET}" + echo -e "\t${COLOR_RED}Error de conexión con 8.8.8.8${COLOR_RESET}" + + # Obtener la interfaz predeterminada + default_interface=$(ip route show default | awk '/default/ {print $5}') + + # Probar la conexión con el router + router_ip=$(get_router_ip "$default_interface") + if ping -c 1 -W 2 "$router_ip" > /dev/null 2>&1; then + echo -e "\t${COLOR_GREEN}Conexión exitosa con el router ($router_ip)${COLOR_RESET}" + else + echo -e "\t${COLOR_RED}Error de conexión con el router ($router_ip)${COLOR_RESET}" + exit 1ç + fi fi } -# Iterar sobre la lista de sitios web -for site in "${websites[@]}"; do - check_website_status "$site" -done +# Comprobar estado de sitios web +check_websites() { + # Lista de sitios web a comprobar + websites=("vergaracarmona.es" "gitea.vergaracarmona.es" "diariosenderista.es" "marialuisasefardi.es" "prefapp.es") -# Mostrar información de tarjetas de red -print_title "TARJETAS DE RED" -while read -r interface; do - network_info+=("$(print_network_info "$interface")") -done < <(ip -o link show | awk -F': ' '{print $2}') + # Verificar el estado de sitios web + print_title "VERIFICANDO ESTADO DE WEBS" + check_website_status() { + local website=$1 + local timeout=5 + local http_code=$(curl -o /dev/null -L -s -w "%{http_code}" --max-time "$timeout" "$website") -# Mostrar la tabla de información de red -print_network_table + if [ "$http_code" -eq 200 ]; then + echo -e "\t${COLOR_GREEN}$website está levantado. ${COLOR_RESET}" + else + echo -e "\t${COLOR_RED}$website está caído (Código $http_code)${COLOR_RESET}" + fi + } -# Mostrar información de IP WAN -print_title "INFO IP WAN" -curl -s https://ipinfo.io | jq + # Iterar sobre la lista de sitios web + for site in "${websites[@]}"; do + check_website_status "$site" + done +} -# Mostrar estado de NordVPN -print_title "ESTADO NORDVPN" -nordvpn status | grep -v "New feature" -# Realizar test de velocidad -print_title "TEST DE VELOCIDAD" -speedtest +# Obtener las interfaces de red +get_interfaces() { + print_title "TARJETAS DE RED" + while read -r interface; do + network_info+=("$(print_network_info "$interface")") + done < <(ip -o link show | awk -F': ' '{print $2}') + + print_network_table +} + +# Obtener IP pública +get_public_ip_info() { + print_title "INFO IP WAN" + curl -s https://ipinfo.io | jq -r 'to_entries[] | "\u001b[34m\(.key):\u001b[0m \u001b[32m\(.value)\u001b[0m"' +} + +# Estado de nordvpn +nordvpn_status() { + print_title "ESTADO NORDVPN" + nordvpn status | grep -v "New feature" +} + +# Test de velocidad +speed_test() { + print_title "TEST DE VELOCIDAD" + speedtest +} # Realizar traceroute -print_title "TRACEROUTE" -mtr -c 3 -n -r 8.8.8.8 +traceroute(){ + print_title "TRACEROUTE" + mtr -c 3 -n -r 8.8.8.8 +} +# Menú principal +main_menu() { + while true; do + print_title "Menú" + echo "1 - Comprobar conexión" + echo "2 - Verificar estado de webs" + echo "3 - Ver tarjetas de red" + echo "4 - Información de IP pública" + echo "5 - Estado de NordVPN" + echo "6 - Test de velocidad" + echo "7 - Traceroute" + echo "8 - Salir" + + read -p "Ingrese el número de la opción deseada: " choice + case $choice in + 1) check_connection ;; + 2) check_websites ;; + 3) get_interfaces ;; + 4) get_public_ip_info ;; + 5) nordvpn_status ;; + 6) speed_test ;; + 7) traceroute ;; + 8) echo -e "${COLOR_RED}\nSaliendo...\n${COLOR_RESET}"; tput cnorm; exit 0 ;; + *) echo -e "${COLOR_RED}\nOpción inválida. Inténtelo de nuevo.${COLOR_RESET}" ;; + esac + done +} + +# Función principal +main() { + check_connection + check_websites + get_interfaces + get_public_ip_info + nordvpn_status + main_menu +} + +main \ No newline at end of file