ProxMenux/scripts/repair_network.sh

219 lines
8.1 KiB
Bash
Raw Normal View History

2025-01-12 12:04:56 +01:00
#!/bin/bash
2025-02-02 11:06:51 +01:00
# ==========================================================
# ProxMenu - A menu-driven script for Proxmox VE management
# ==========================================================
# Author : MacRimi
# Copyright : (c) 2024 MacRimi
# License : MIT (https://raw.githubusercontent.com/MacRimi/ProxMenux/main/LICENSE)
# Version : 1.0
# Last Updated: 28/01/2025
# ==========================================================
# Description:
# This script allows users to assign physical disks for passthrough to existing
# Proxmox virtual machines (VMs) through an interactive menu.
# - Detects and lists physical and network interfaces.
# - Verifies and repairs bridge configurations.
# - Ensures network connectivity by checking IP assignments.
# - Provides options to manually repair or verify network settings.
# - Offers interactive menus for user-friendly operation.
#
# The script aims to simplify network troubleshooting and ensure
# that Proxmox systems maintain stable connectivity.
# ==========================================================
# Configuration ============================================
REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main"
2025-01-12 13:40:38 +01:00
BASE_DIR="/usr/local/share/proxmenux"
2025-02-02 11:06:51 +01:00
UTILS_FILE="$BASE_DIR/utils.sh"
VENV_PATH="/opt/googletrans-env"
if [[ -f "$UTILS_FILE" ]]; then
source "$UTILS_FILE"
2025-01-12 12:04:56 +01:00
fi
2025-02-02 11:06:51 +01:00
load_language
initialize_cache
# ==========================================================
2025-01-12 12:04:56 +01:00
2025-02-02 11:06:51 +01:00
# Function to detect physical network interfaces
detect_physical_interfaces() {
2025-02-02 11:48:53 +01:00
2025-02-02 11:06:51 +01:00
physical_interfaces=$(ip -o link show | awk -F': ' '$2 !~ /^(lo|veth|dummy|bond)/ {print $2}')
whiptail --title "$(translate 'Network Interfaces')" --msgbox "$physical_interfaces" 10 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to get all relevant network interfaces (physical and bridges)
get_relevant_interfaces() {
echo $(ip -o link show | awk -F': ' '$2 !~ /^(lo|veth|dummy)/ {print $2}')
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to check and fix bridge configuration
2025-01-12 12:04:56 +01:00
check_and_fix_bridges() {
2025-02-02 11:48:53 +01:00
2025-01-12 17:42:45 +01:00
local output=""
2025-02-02 11:06:51 +01:00
output+="$(translate 'Checking bridges')\\n\\n"
2025-01-12 12:04:56 +01:00
bridges=$(grep "^auto vmbr" /etc/network/interfaces | awk '{print $2}')
for bridge in $bridges; do
old_port=$(grep -A1 "iface $bridge" /etc/network/interfaces | grep "bridge-ports" | awk '{print $2}')
if ! ip link show "$old_port" &>/dev/null; then
2025-02-02 11:06:51 +01:00
output+="$(translate 'Bridge port missing'): $bridge - $old_port\\n"
2025-01-12 12:04:56 +01:00
new_port=$(echo "$physical_interfaces" | tr ' ' '\n' | grep -v "vmbr" | head -n1)
if [ -n "$new_port" ]; then
sed -i "/iface $bridge/,/bridge-ports/ s/bridge-ports.*/bridge-ports $new_port/" /etc/network/interfaces
2025-02-02 11:06:51 +01:00
output+="$(translate 'Bridge port updated'): $bridge - $old_port -> $new_port\\n"
2025-01-12 12:04:56 +01:00
else
2025-02-02 11:06:51 +01:00
output+="$(translate 'No physical interface available')\\n"
2025-01-12 12:04:56 +01:00
fi
else
2025-02-02 11:06:51 +01:00
output+="$(translate 'Bridge port OK'): $bridge - $old_port\\n"
2025-01-12 12:04:56 +01:00
fi
done
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Checking Bridges')" --msgbox "$output" 20 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
2025-01-12 12:04:56 +01:00
clean_nonexistent_interfaces() {
2025-02-02 11:48:53 +01:00
2025-01-12 17:42:45 +01:00
local output=""
2025-02-02 11:06:51 +01:00
output+="$(translate 'Cleaning interfaces')\\n\\n"
configured_interfaces=$(grep "^iface" /etc/network/interfaces | awk '{print $2}' | grep -v "lo")
2025-01-12 12:04:56 +01:00
for iface in $configured_interfaces; do
2025-02-02 11:06:51 +01:00
if [[ ! $iface =~ ^(vmbr|bond) ]] && ! ip link show "$iface" &>/dev/null; then
2025-01-12 12:04:56 +01:00
sed -i "/iface $iface/,/^$/d" /etc/network/interfaces
2025-02-02 11:06:51 +01:00
output+="$(translate 'Interface removed'): $iface\\n"
2025-01-12 12:04:56 +01:00
fi
done
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Cleaning Interfaces')" --msgbox "$output" 15 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Update other functions to use physical_interfaces or get_relevant_interfaces as appropriate
2025-01-12 12:04:56 +01:00
configure_physical_interfaces() {
2025-02-02 11:48:53 +01:00
2025-01-12 17:42:45 +01:00
local output=""
2025-02-02 11:06:51 +01:00
output+="$(translate 'Configuring interfaces')\\n\\n"
2025-01-12 12:04:56 +01:00
for iface in $physical_interfaces; do
if ! grep -q "iface $iface" /etc/network/interfaces; then
echo -e "\niface $iface inet manual" >> /etc/network/interfaces
2025-02-02 11:06:51 +01:00
output+="$(translate 'Interface added'): $iface\\n"
2025-01-12 12:04:56 +01:00
fi
done
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Configuring Interfaces')" --msgbox "$output" 15 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to restart networking service
2025-01-12 12:04:56 +01:00
restart_networking() {
2025-02-02 11:06:51 +01:00
if (whiptail --title "$(translate 'Restarting Network')" --yesno "$(translate 'Do you want to restart the network service?')" 10 60); then
2025-02-02 11:48:53 +01:00
clear
msg_info "$(translate 'The network service is about to restart. You may experience a brief disconnection.')"
2025-01-12 17:42:45 +01:00
systemctl restart networking
if [ $? -eq 0 ]; then
2025-02-02 11:06:51 +01:00
msg_ok "$(translate 'Network service restarted successfully')"
2025-01-12 17:42:45 +01:00
else
2025-02-02 11:06:51 +01:00
msg_error "$(translate 'Failed to restart network service')"
2025-01-12 17:42:45 +01:00
fi
2025-01-12 12:04:56 +01:00
else
2025-02-02 11:48:53 +01:00
msg_ok "$(translate 'Network restart canceled')"
2025-01-12 12:04:56 +01:00
fi
}
2025-02-02 11:06:51 +01:00
# Function to check network connectivity
2025-01-12 12:04:56 +01:00
check_network_connectivity() {
if ping -c 4 8.8.8.8 &> /dev/null; then
2025-02-02 11:06:51 +01:00
msg_ok "$(translate 'Network connectivity OK')"
2025-01-12 12:04:56 +01:00
return 0
else
2025-02-02 11:48:53 +01:00
msg_error "$(translate 'Network connectivity failed')"
2025-01-12 12:04:56 +01:00
return 1
fi
}
2025-02-02 11:06:51 +01:00
# Update the show_ip_info function to use the new get_relevant_interfaces function
2025-01-12 12:04:56 +01:00
show_ip_info() {
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'IP Information')" --infobox "$(translate 'Gathering IP information...')" 8 78
2025-01-12 17:42:45 +01:00
local ip_info=""
2025-02-02 11:06:51 +01:00
ip_info+="$(translate 'IP Information')\\n\\n"
2025-02-02 11:48:53 +01:00
2025-02-02 11:06:51 +01:00
local interfaces=$(get_relevant_interfaces)
2025-02-02 11:48:53 +01:00
2025-02-02 11:06:51 +01:00
for interface in $interfaces; do
local interface_ip=$(ip -4 addr show $interface 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
2025-01-12 17:42:45 +01:00
if [ -n "$interface_ip" ]; then
2025-02-02 11:06:51 +01:00
ip_info+="$interface: $interface_ip\\n"
2025-01-12 12:04:56 +01:00
else
2025-02-02 11:06:51 +01:00
ip_info+="$interface: $(translate 'No IP assigned')\\n"
2025-01-12 12:04:56 +01:00
fi
done
2025-02-02 11:48:53 +01:00
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Result')" --msgbox "${ip_info}\\n\\n$(translate 'IP information gathering completed')\\n\\n$(translate 'Press Enter to continue')" 20 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to repair network
2025-01-12 12:04:56 +01:00
repair_network() {
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Network Repair Started')" --infobox "$(translate 'Repairing network...')" 8 78
2025-02-02 11:48:53 +01:00
echo -ne "${TAB}${YW}-$(translate 'Repairing network...') ${CL}"
sleep 3
2025-01-12 12:04:56 +01:00
detect_physical_interfaces
clean_nonexistent_interfaces
check_and_fix_bridges
configure_physical_interfaces
restart_networking
if check_network_connectivity; then
show_ip_info
2025-02-02 11:06:51 +01:00
msg_ok "$(translate 'Network repair completed successfully')"
2025-01-12 12:04:56 +01:00
else
2025-02-02 11:06:51 +01:00
msg_error "$(translate 'Network repair failed')"
2025-01-12 12:04:56 +01:00
fi
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Result')" --msgbox "$(translate 'Repair process completed')\\n\\n$(translate 'Press Enter to continue')" 10 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to verify network configuration
2025-01-12 12:04:56 +01:00
verify_network() {
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Network Verification Started')" --infobox "$(translate 'Verifying network...')" 8 78
2025-02-02 11:48:53 +01:00
echo -ne "${TAB}${YW}-$(translate 'Verifying network...') ${CL}"
2025-01-12 12:04:56 +01:00
detect_physical_interfaces
show_ip_info
2025-01-12 18:00:50 +01:00
if check_network_connectivity; then
2025-02-02 11:06:51 +01:00
msg_ok "$(translate 'Network verification completed successfully')"
2025-01-12 18:00:50 +01:00
else
2025-02-02 11:06:51 +01:00
msg_error "$(translate 'Network verification failed')"
2025-01-12 18:00:50 +01:00
fi
2025-02-02 11:06:51 +01:00
whiptail --title "$(translate 'Result')" --msgbox "$(translate 'Verification process completed')\\n\\n$(translate 'Press Enter to continue')" 10 78
2025-01-12 12:04:56 +01:00
}
2025-02-02 11:06:51 +01:00
# Function to show main menu
2025-01-12 17:42:45 +01:00
show_main_menu() {
2025-01-12 12:04:56 +01:00
while true; do
2025-02-02 11:06:51 +01:00
OPTION=$(whiptail --title "$(translate 'Network Repair Menu')" --menu "$(translate 'Choose an option:')" 15 60 4 \
"1" "$(translate 'Repair Network')" \
"2" "$(translate 'Verify Network')" \
"3" "$(translate 'Show IP Information')" \
2025-02-02 11:57:42 +01:00
"4" "$(translate "Return to Main Menu")" 3>&1 1>&2 2>&3)
2025-01-12 12:04:56 +01:00
case $OPTION in
1)
repair_network
;;
2)
verify_network
;;
3)
show_ip_info
;;
2025-02-02 11:57:42 +01:00
4) exec bash <(curl -s "$REPO_URL/scripts/menus/main_menu.sh") ;;
*) exec bash <(curl -s "$REPO_URL/scripts/menus/main_menu.sh") ;;
2025-01-12 12:04:56 +01:00
esac
done
}
2025-02-02 11:48:53 +01:00
clear
2025-02-04 17:18:11 +01:00
show_proxmenu_logo "$YW"
2025-02-02 11:06:51 +01:00
show_main_menu