scripts-random/01_bash/pruebaRed.sh

338 lines
9.5 KiB
Bash
Raw Permalink Normal View History

2024-02-22 21:02:37 +01:00
#!/bin/bash
2024-03-08 16:10:11 +01:00
# Script: pruebared.sh
# Descripción: Este script de bash tiene como objetivo
# Author: Manuel Vergara
# Web: https://vergaracarmona.es
#
2024-02-22 21:02:37 +01:00
# Colores
COLOR_GREEN='\e[32m'
COLOR_RED='\e[31m'
COLOR_YELLOW='\e[33m'
COLOR_BLUE='\e[34m'
2024-03-08 16:10:11 +01:00
COLOR_CYAN='\e[36m'
2024-02-22 21:02:37 +01:00
COLOR_RESET='\e[0m'
2024-03-08 16:10:11 +01:00
# Banderas para indicar si faltan dependencias
curl_missing=false
ip_missing=false
jq_missing=false
mtr_missing=false
nordvpn_missing=false
ping_missing=false
speedtest_missing=false
# Webs a comprobar
websites=("vergaracarmona.es" "gitea.vergaracarmona.es" "diariosenderista.es" "marialuisasefardi.es" "prefapp.es")
2024-02-22 21:02:37 +01:00
# Control de salida con Ctrl + C
2024-03-08 16:10:11 +01:00
ctrl_c() {
echo -e "${COLOR_RED}\n\n❗ Saliendo... \n${COLOR_RESET}"
2024-02-25 22:40:37 +01:00
tput cnorm; exit 1
2024-02-22 21:02:37 +01:00
}
# Ctrl+C
trap ctrl_c SIGINT
2024-03-08 16:10:11 +01:00
# Comprobación de dependencias
check_dependencies() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo -e "${COLOR_RED}🛂 El comando $cmd no está instalado.${COLOR_RESET}"
case $cmd in
curl)
echo -e "${COLOR_CYAN}Más info: https://vergaracarmona.es/ejemplos-de-comando-curl/${COLOR_RESET}"
curl_missing=true
;;
ip)
echo -e "${COLOR_CYAN}Más info: https://wiki.linuxfoundation.org/networking/iproute2${COLOR_RESET}"
ip_missing=true
;;
jq)
echo -e "${COLOR_CYAN}Más info: https://vergaracarmona.es/guia-del-comando-jq/${COLOR_RESET}"
jq_missing=true
;;
mtr)
echo -e "${COLOR_CYAN}Más info: https://www.bitwizard.nl/mtr/${COLOR_RESET}"
mtr_missing=true
;;
nordvpn)
echo -e "${COLOR_CYAN}Más info: https://go.nordvpn.net/aff_c?offer_id=15&aff_id=45752&url_id=11987${COLOR_RESET}"
nordvpn_missing=true
;;
ping)
echo -e "${COLOR_CYAN}Más info: https://packages.debian.org/sid/iputils-ping${COLOR_RESET}"
ping_missing=true
;;
speedtest)
echo -e "${COLOR_CYAN}Más info: https://www.speedtest.net/apps/cli${COLOR_RESET}"
speedtest_missing=true
;;
esac
fi
}
2024-02-22 21:02:37 +01:00
# Títulos
2024-03-08 16:10:11 +01:00
print_title() {
2024-02-22 21:02:37 +01:00
local title="$1"
local title_length=${#title}
local total_length=22
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
# Calcular el tamaño del margen izquierdo y derecho
2024-02-25 22:40:37 +01:00
local margin_length=$(( (total_length - title_length - 4) / 2 ))
2024-02-22 21:02:37 +01:00
# Asegurarse de que el número total de almohadillas sea siempre 22
2024-02-25 22:40:37 +01:00
local left_padding=$(( margin_length ))
local right_padding=$(( margin_length + (title_length % 2) ))
2024-02-22 21:02:37 +01:00
echo -e "${COLOR_BLUE}\n$(printf '#%.0s' $(seq 1 $left_padding))#### $title ####$(printf '#%.0s' $(seq 1 $right_padding))\n${COLOR_RESET}"
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# 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}')
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
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
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# 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}"
}
# Función para obtener la IP del router
get_router_ip() {
local interface=$1
ip route show dev "$interface" | grep default | awk '{print $3}'
}
# Comprobar conexión
check_connection() {
2024-03-08 16:10:11 +01:00
print_title "🔍 COMPROBAR CONEXIÓN"
# Lista de comandos a verificar
commands=("ping" "ip")
# Verificar la instalación de cada comando de la lista
for cmd in "${commands[@]}"; do
if [ "${cmd}_missing" = true ]; then
check_dependencies "$cmd"
return
fi
done
2024-02-25 22:40:37 +01:00
if ping -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then
2024-02-22 21:02:37 +01:00
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}"
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
# Obtener la interfaz predeterminada
default_interface=$(ip route show default | awk '/default/ {print $5}')
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
# Probar la conexión con el router
router_ip=$(get_router_ip "$default_interface")
2024-02-25 22:40:37 +01:00
if ping -c 1 -W 2 "$router_ip" > /dev/null 2>&1; then
2024-02-22 21:02:37 +01:00
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
fi
fi
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Comprobar estado de sitios web
check_websites() {
# Lista de sitios web a comprobar
2024-03-08 16:10:11 +01:00
local websites=("$@")
2024-02-22 21:02:37 +01:00
# Verificar el estado de sitios web
2024-03-08 16:10:11 +01:00
print_title "👀 VERIFICANDO ESTADO DE WEBS"
# Verificar si el comando curl está instalado
if [ "$curl_missing" = true ]; then
check_dependencies "curl"
return
fi
2024-02-22 21:02:37 +01:00
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")
if [ "$http_code" -eq 200 ]; then
2024-03-08 16:10:11 +01:00
echo -e "\t✅ ${COLOR_GREEN}$website funciona correctamente.\n${COLOR_RESET}"
2024-02-22 21:02:37 +01:00
else
2024-03-08 16:10:11 +01:00
echo -e "\t❌ ${COLOR_RED}$website tiene error con el código $http_code.\n${COLOR_RESET}"
2024-02-22 21:02:37 +01:00
fi
}
# Iterar sobre la lista de sitios web
for site in "${websites[@]}"; do
check_website_status "$site"
done
}
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
# Obtener las interfaces de red
get_interfaces() {
2024-03-08 16:10:11 +01:00
# Verificar si el comando ip está instalado
if [ "$ip_missing" = true ]; then
check_dependencies "ip"
return
fi
network_info=()
print_title " TARJETAS DE RED"
2024-02-22 21:02:37 +01:00
while read -r interface; do
network_info+=("$(print_network_info "$interface")")
2024-03-11 16:05:10 +01:00
done < <(ip -o link show | awk -F': ' '{sub(/@.*/, "", $2); print $2}')
2024-02-22 21:02:37 +01:00
print_network_table
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Obtener IP pública
get_public_ip_info() {
2024-03-08 16:10:11 +01:00
# Verificar si el comando curl está instalado
if [ "$curl_missing" = true ]; then
check_dependencies "curl"
return
fi
print_title " INFO IP WAN"
curl -s https://ipinfo.io | jq -r 'to_entries[] | "\u001b[34m\(.key):\u001b[0m \u001b[32m\(.value)\u001b[0m"'
2024-02-22 21:02:37 +01:00
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Estado de nordvpn
nordvpn_status() {
2024-03-08 16:10:11 +01:00
print_title " ESTADO NORDVPN"
# Verificar si el comando nordvpn está instalado
if [ "$nordvpn_missing" = true ]; then
check_dependencies "nordvpn"
return
fi
2024-02-22 21:02:37 +01:00
nordvpn status | grep -v "New feature"
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Nueva conexión nordvpn
nordvpn_connect() {
2024-03-08 16:10:11 +01:00
print_title "🔌 CONECTANDO NORDVPN"
# Verificar si el comando nordvpn está instalado
if [ "$nordvpn_missing" = true ]; then
check_dependencies "nordvpn"
return
fi
2024-02-25 22:40:37 +01:00
nordvpn connect Spain Barcelona
2024-02-22 21:02:37 +01:00
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Test de velocidad
speed_test() {
2024-03-08 16:10:11 +01:00
print_title "🚅 TEST DE VELOCIDAD"
# Verificar si el comando speedtest está instalado
if [ "$speedtest_missing" = true ]; then
check_dependencies "speedtest"
return
fi
2024-02-22 21:02:37 +01:00
speedtest
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Realizar traceroute
2024-02-25 22:40:37 +01:00
traceroute(){
2024-03-08 16:10:11 +01:00
print_title "🔍 TRACEROUTE"
# Verificar si el comando mtr está instalado
if [ "$mtr_missing" = true ]; then
check_dependencies "mtr"
return
fi
2024-02-22 21:02:37 +01:00
mtr -c 3 -n -r 8.8.8.8
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Menú principal
main_menu() {
while true; do
2024-03-08 16:10:11 +01:00
print_title "📋 Menú"
2024-02-22 21:02:37 +01:00
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 - Conectar Nordvpn"
2024-02-25 22:40:37 +01:00
echo "7 - Test de velocidad"
2024-02-22 21:02:37 +01:00
echo "8 - Traceroute"
echo "9 - Salir"
2024-02-25 22:40:37 +01:00
2024-02-22 21:02:37 +01:00
read -p "Ingrese el número de la opción deseada: " choice
case $choice in
2024-02-25 22:40:37 +01:00
1) check_connection ;;
2024-03-11 15:40:44 +01:00
2) check_websites "${websites[@]}" ;;
2024-02-25 22:40:37 +01:00
3) get_interfaces ;;
4) get_public_ip_info ;;
5) nordvpn_status ;;
6) nordvpn_connect ;;
2024-03-08 16:10:11 +01:00
7) speed_test ;;
2024-02-25 22:40:37 +01:00
8) traceroute ;;
2024-03-08 16:10:11 +01:00
9|exit|quit) echo -e "${COLOR_RED}\n\n❗ Saliendo... \n${COLOR_RESET}"; tput cnorm; exit 0 ;;
*) echo -e "${COLOR_RED}\n❓ Opción inválida. Inténtelo de nuevo.${COLOR_RESET}" ;;
2024-02-22 21:02:37 +01:00
esac
done
}
2024-03-08 16:10:11 +01:00
2024-02-22 21:02:37 +01:00
# Función principal
main() {
2024-03-08 16:10:11 +01:00
for cmd in "curl" "ip" "jq" "mtr" "nordvpn" "ping" "speedtest"; do
check_dependencies "$cmd"
done
2024-02-22 21:02:37 +01:00
check_connection
2024-03-08 16:10:11 +01:00
check_websites "${websites[@]}"
2024-02-22 21:02:37 +01:00
get_interfaces
get_public_ip_info
nordvpn_status
main_menu
}
main
2024-03-08 16:10:11 +01:00