Update pruebaRed.sh

This commit is contained in:
Manuel Vergara 2024-03-08 16:10:11 +01:00
parent f31d48ca7a
commit 8008cca3b1

View File

@ -1,23 +1,83 @@
#!/bin/bash #!/bin/bash
# Script: pruebared.sh
# Descripción: Este script de bash tiene como objetivo
# Author: Manuel Vergara
# Web: https://vergaracarmona.es
#
# Colores # Colores
COLOR_GREEN='\e[32m' COLOR_GREEN='\e[32m'
COLOR_RED='\e[31m' COLOR_RED='\e[31m'
COLOR_YELLOW='\e[33m' COLOR_YELLOW='\e[33m'
COLOR_BLUE='\e[34m' COLOR_BLUE='\e[34m'
COLOR_CYAN='\e[36m'
COLOR_RESET='\e[0m' COLOR_RESET='\e[0m'
# 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")
# Control de salida con Ctrl + C # Control de salida con Ctrl + C
function ctrl_c() { ctrl_c() {
echo -e "${COLOR_RED}\n\n[!] Saliendo... \n${COLOR_RESET}" echo -e "${COLOR_RED}\n\n Saliendo... \n${COLOR_RESET}"
tput cnorm; exit 1 tput cnorm; exit 1
} }
# Ctrl+C # Ctrl+C
trap ctrl_c SIGINT trap ctrl_c SIGINT
# 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
}
# Títulos # Títulos
function print_title { print_title() {
local title="$1" local title="$1"
local title_length=${#title} local title_length=${#title}
local total_length=22 local total_length=22
@ -32,6 +92,7 @@ function print_title {
echo -e "${COLOR_BLUE}\n$(printf '#%.0s' $(seq 1 $left_padding))#### $title ####$(printf '#%.0s' $(seq 1 $right_padding))\n${COLOR_RESET}" 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 # Función para imprimir información de red
print_network_info() { print_network_info() {
local interface=$1 local interface=$1
@ -48,6 +109,7 @@ print_network_info() {
fi fi
} }
# Función para imprimir la tabla de información de red # Función para imprimir la tabla de información de red
print_network_table() { print_network_table() {
echo -e "\n${COLOR_BLUE}| Interfaz | IP |${COLOR_RESET}" echo -e "\n${COLOR_BLUE}| Interfaz | IP |${COLOR_RESET}"
@ -60,12 +122,6 @@ print_network_table() {
echo -e "${COLOR_BLUE}| ---------------- | ----------------------------------- |${COLOR_RESET}" echo -e "${COLOR_BLUE}| ---------------- | ----------------------------------- |${COLOR_RESET}"
} }
# Comprobación de dependencias
check_dependencies() {
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 # Función para obtener la IP del router
get_router_ip() { get_router_ip() {
@ -73,12 +129,22 @@ get_router_ip() {
ip route show dev "$interface" | grep default | awk '{print $3}' ip route show dev "$interface" | grep default | awk '{print $3}'
} }
# Inicializar el array para almacenar información de red
network_info=()
# Comprobar conexión # Comprobar conexión
check_connection() { check_connection() {
print_title "COMPROBAR CONEXIÓN" 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
if ping -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then 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}" echo -e "\t${COLOR_GREEN}Conexión exitosa con 8.8.8.8${COLOR_RESET}"
else else
@ -98,22 +164,30 @@ check_connection() {
fi fi
} }
# Comprobar estado de sitios web # Comprobar estado de sitios web
check_websites() { check_websites() {
# Lista de sitios web a comprobar # Lista de sitios web a comprobar
websites=("vergaracarmona.es" "gitea.vergaracarmona.es" "diariosenderista.es" "marialuisasefardi.es" "prefapp.es") local websites=("$@")
# Verificar el estado de sitios web # Verificar el estado de sitios web
print_title "VERIFICANDO ESTADO DE WEBS" print_title "👀 VERIFICANDO ESTADO DE WEBS"
# Verificar si el comando curl está instalado
if [ "$curl_missing" = true ]; then
check_dependencies "curl"
return
fi
check_website_status() { check_website_status() {
local website=$1 local website=$1
local timeout=5 local timeout=5
local http_code=$(curl -o /dev/null -L -s -w "%{http_code}" --max-time "$timeout" "$website") local http_code=$(curl -o /dev/null -L -s -w "%{http_code}" --max-time "$timeout" "$website")
if [ "$http_code" -eq 200 ]; then if [ "$http_code" -eq 200 ]; then
echo -e "\t${COLOR_GREEN}$website está levantado. ${COLOR_RESET}" echo -e "\t${COLOR_GREEN}$website funciona correctamente.\n${COLOR_RESET}"
else else
echo -e "\t${COLOR_RED}$website está caído (Código $http_code)${COLOR_RESET}" echo -e "\t${COLOR_RED}$website tiene error con el código $http_code.\n${COLOR_RESET}"
fi fi
} }
@ -126,7 +200,15 @@ check_websites() {
# Obtener las interfaces de red # Obtener las interfaces de red
get_interfaces() { get_interfaces() {
print_title "TARJETAS DE RED"
# Verificar si el comando ip está instalado
if [ "$ip_missing" = true ]; then
check_dependencies "ip"
return
fi
network_info=()
print_title " TARJETAS DE RED"
while read -r interface; do while read -r interface; do
network_info+=("$(print_network_info "$interface")") network_info+=("$(print_network_info "$interface")")
done < <(ip -o link show | awk -F': ' '{print $2}') done < <(ip -o link show | awk -F': ' '{print $2}')
@ -134,40 +216,83 @@ get_interfaces() {
print_network_table print_network_table
} }
# Obtener IP pública # Obtener IP pública
get_public_ip_info() { get_public_ip_info() {
print_title "INFO IP WAN"
curl -s https://ipinfo.io | jq -r 'to_entries[] | select(.key != "readme") | "\u001b[34m\(.key):\u001b[0m \u001b[32m\(.value)\u001b[0m"' # 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"'
} }
# Estado de nordvpn # Estado de nordvpn
nordvpn_status() { nordvpn_status() {
print_title "ESTADO NORDVPN"
print_title " ESTADO NORDVPN"
# Verificar si el comando nordvpn está instalado
if [ "$nordvpn_missing" = true ]; then
check_dependencies "nordvpn"
return
fi
nordvpn status | grep -v "New feature" nordvpn status | grep -v "New feature"
} }
# Nueva conexión nordvpn # Nueva conexión nordvpn
nordvpn_connect() { nordvpn_connect() {
print_title "CONECTANDO NORDVPN" print_title "🔌 CONECTANDO NORDVPN"
# Verificar si el comando nordvpn está instalado
if [ "$nordvpn_missing" = true ]; then
check_dependencies "nordvpn"
return
fi
nordvpn connect Spain Barcelona nordvpn connect Spain Barcelona
} }
# Test de velocidad # Test de velocidad
speed_test() { speed_test() {
print_title "TEST DE VELOCIDAD"
print_title "🚅 TEST DE VELOCIDAD"
# Verificar si el comando speedtest está instalado
if [ "$speedtest_missing" = true ]; then
check_dependencies "speedtest"
return
fi
speedtest speedtest
} }
# Realizar traceroute # Realizar traceroute
traceroute(){ traceroute(){
print_title "TRACEROUTE" print_title "🔍 TRACEROUTE"
# Verificar si el comando mtr está instalado
if [ "$mtr_missing" = true ]; then
check_dependencies "mtr"
return
fi
mtr -c 3 -n -r 8.8.8.8 mtr -c 3 -n -r 8.8.8.8
} }
# Menú principal # Menú principal
main_menu() { main_menu() {
while true; do while true; do
print_title "Menú" print_title "📋 Menú"
echo "1 - Comprobar conexión" echo "1 - Comprobar conexión"
echo "2 - Verificar estado de webs" echo "2 - Verificar estado de webs"
echo "3 - Ver tarjetas de red" echo "3 - Ver tarjetas de red"
@ -186,18 +311,22 @@ main_menu() {
4) get_public_ip_info ;; 4) get_public_ip_info ;;
5) nordvpn_status ;; 5) nordvpn_status ;;
6) nordvpn_connect ;; 6) nordvpn_connect ;;
7) speed_test ;; 7) speed_test ;;
8) traceroute ;; 8) traceroute ;;
9) echo -e "${COLOR_RED}\nSaliendo...\n${COLOR_RESET}"; tput cnorm; exit 0 ;; 9|exit|quit) echo -e "${COLOR_RED}\n\n❗ Saliendo... \n${COLOR_RESET}"; tput cnorm; exit 0 ;;
*) echo -e "${COLOR_RED}\nOpción inválida. Inténtelo de nuevo.${COLOR_RESET}" ;; *) echo -e "${COLOR_RED}\nOpción inválida. Inténtelo de nuevo.${COLOR_RESET}" ;;
esac esac
done done
} }
# Función principal # Función principal
main() { main() {
for cmd in "curl" "ip" "jq" "mtr" "nordvpn" "ping" "speedtest"; do
check_dependencies "$cmd"
done
check_connection check_connection
check_websites check_websites "${websites[@]}"
get_interfaces get_interfaces
get_public_ip_info get_public_ip_info
nordvpn_status nordvpn_status
@ -205,3 +334,4 @@ main() {
} }
main main