diff --git a/src/pruebaRed.sh b/src/pruebaRed.sh index fc3d571..ed160e8 100755 --- a/src/pruebaRed.sh +++ b/src/pruebaRed.sh @@ -1,87 +1,143 @@ #!/bin/bash -# Script comprobación de red +# Colores +COLOR_GREEN='\e[32m' +COLOR_RED='\e[31m' +COLOR_YELLOW='\e[33m' +COLOR_BLUE='\e[34m' +COLOR_RESET='\e[0m' -# Conexión con google +# Control de salida con Ctrl + C +function ctrl_c() { + echo -e "${COLOR_RED}\n\n[!] Saliendo... \n${COLOR_RESET}" + tput cnorm; exit 1 +} -echo -e "\n###################\nCONEXIÓN CON GOOGLE\n###################" -response=$(ping -c 1 8.8.8.8 | grep '1 packets transmitted, 1 received') +# Ctrl+C +trap ctrl_c SIGINT -if [ -n "$response" ]; then - echo "Conexión exitosa con 8.8.8.8" +# Títulos +function print_title { + local title="$1" + local title_length=${#title} + local total_length=22 + + # Calcular el tamaño del margen izquierdo y derecho + local margin_length=$(( (total_length - title_length - 4) / 2 )) + + # Asegurarse de que el número total de almohadillas sea siempre 22 + local left_padding=$(( margin_length )) + local right_padding=$(( margin_length + (title_length % 2) )) + + echo -e "${COLOR_BLUE}\n$(printf '#%.0s' $(seq 1 $left_padding))#### $title ####$(printf '#%.0s' $(seq 1 $right_padding))\n${COLOR_RESET}" +} + +# Función para imprimir información de red +print_network_info() { + local interface=$1 + local ip_lan_4=$(ip addr show "$interface" | grep 'inet ' | awk '{print $2}') + local ip_lan_6=$(ip addr show "$interface" | grep 'inet6' | awk '{print $2}') + + if [ -z "$ip_lan_4" ] && [ -z "$ip_lan_6" ]; then + printf "${COLOR_BLUE}|${COLOR_RESET}${COLOR_YELLOW} %-16s ${COLOR_RESET}${COLOR_BLUE}|${COLOR_RESET} ${COLOR_RED}%-35s ${COLOR_RESET}${COLOR_BLUE}|${COLOR_RESET}\n" "$interface" "No tiene" + else + printf "${COLOR_BLUE}|${COLOR_RESET}${COLOR_YELLOW} %-16s ${COLOR_RESET}${COLOR_BLUE}|${COLOR_RESET} ${COLOR_GREEN}%-35s ${COLOR_RESET}${COLOR_BLUE}|${COLOR_RESET}\n" "$interface" "IPv4: $ip_lan_4" + if [ -n "$ip_lan_6" ]; then + printf "${COLOR_BLUE}|${COLOR_RESET} %-16s ${COLOR_BLUE}|${COLOR_RESET} ${COLOR_GREEN}%-35s ${COLOR_RESET}${COLOR_BLUE}|${COLOR_RESET}\n" "" "IPv6: $ip_lan_6" + fi + fi +} + +# Función para imprimir la tabla de información de red +print_network_table() { + echo -e "\n${COLOR_BLUE}| Interfaz | IP |${COLOR_RESET}" + echo -e "${COLOR_BLUE}| ---------------- | ----------------------------------- |${COLOR_RESET}" + + for row in "${network_info[@]}"; do + echo -e "$row" + done + + echo -e "${COLOR_BLUE}| ---------------- | ----------------------------------- |${COLOR_RESET}" +} + +# 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; } +} + +# Función para obtener la IP del router +get_router_ip() { + local interface=$1 + ip route show dev "$interface" | grep default | awk '{print $3}' +} + +# Inicializar el array para almacenar información de red +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 "Error de conexión con 8.8.8.8" -fi - -# IP de la tarjeta de red ethernet -echo -e "\n###############\nTARJETAS DE RED\n###############" -echo -e "eno1" -ip_lan_ethernet_4=$(ip addr show eno1 | grep 'inet ' | awk '{print $2}') -if [ -z "$ip_lan_ethernet_4" ] -then - echo -e "\tNO EXISTE IPv4" -else - echo -e "\tIPv4: $ip_lan_ethernet_4" -fi - -ip_lan_ethernet_6=$(ip addr show eno1 | grep 'inet6' | awk '{print $2}') -if [ -z "$ip_lan_ethernet_6" ] -then - echo -e "\tNO EXISTE IPv6" -else - echo -e "\tIPv6: $ip_lan_ethernet_6" -fi - -# IP de la tarjeta de red wifi -echo -e "wlo1" -ip_lan_wifi_4=$(ip addr show wlo1 | grep 'inet ' | awk '{print $2}') -if [ -z "$ip_lan_wifi_4" ] -then - echo -e "\tNO EXISTE IPv4" -else - echo -e "\tIPv4: $ip_lan_wifi_4" -fi - -ip_lan_wifi_6=$(ip addr show wlo1 | grep 'inet6' | awk '{print $2}') -if [ -z "$ip_lan_wifi_6" ] -then - echo -e "\tNO EXISTE IPv6" -else - echo -e "\tIPv6: $ip_lan_wifi_6" + 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 # Agregar la instrucción exit para detener el script + fi fi -# IP pública -echo -e "\n###########\nINFO IP WAN\n###########" +# 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}" + fi +} + +# Iterar sobre la lista de sitios web +for site in "${websites[@]}"; do + check_website_status "$site" +done + +# 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}') + +# Mostrar la tabla de información de red +print_network_table + +# Mostrar información de IP WAN +print_title "INFO IP WAN" curl -s https://ipinfo.io | jq -# Estado VPN -echo -e "\n##############\nESTADO NORDVPN\n##############" +# Mostrar estado de NordVPN +print_title "ESTADO NORDVPN" nordvpn status | grep -v "New feature" -# test de velocidad -echo -e "\n#################\nTEST DE VELOCIDAD\n#################" +# Realizar test de velocidad +print_title "TEST DE VELOCIDAD" speedtest -# Verificación de sitios web -echo -e "\n##########################\nVERIFICANDO ESTADO DE WEBS\n##########################" -ping -c 1 vergaracarmona.es > /dev/null 2>&1 -if [ $? -eq 0 ] -then - echo -e "\tvergaracarmona.es está levantado" -else - echo -e "\tvergaracarmona.es está caído" -fi - -ping -c 1 diariosenderista.es > /dev/null 2>&1 -if [ $? -eq 0 ] -then - echo -e "\tdiariosenderista.es está levantado" -else - echo -e "\tdiariosenderista.es está caído" -fi - -# Traceroute -echo -e "\n##########\nTRACEROUTE\n##########" +# Realizar traceroute +print_title "TRACEROUTE" mtr -c 3 -n -r 8.8.8.8