#!/bin/bash # Enhanced network configuration with validation and rollback support # Network interface detection detect_network_interface() { local interface # Try to detect active interface interface=$(ip route show default | awk 'NR==1 {print $5}') if [[ -z "$interface" ]]; then # Fallback to first available interface interface=$(ip link show | awk -F: '$0 !~ "lo|vir|docker|br-"{print $2; exit}' | tr -d ' ') fi if [[ -z "$interface" ]]; then log_error "No network interface detected" return 1 fi echo "$interface" return 0 } # Get current network configuration get_current_network_config() { local interface="$1" local current_ip gateway_ip dns_servers # Get current IP current_ip=$(ip addr show "$interface" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1) # Get current gateway gateway_ip=$(ip route show default | awk 'NR==1 {print $3}') # Get DNS servers if [[ -f /etc/resolv.conf ]]; then dns_servers=$(awk '/^nameserver/ {print $2}' /etc/resolv.conf | head -n2 | tr '\n' ',' | sed 's/,$//') fi echo "Current IP: ${current_ip:-"Not set"}" echo "Current Gateway: ${gateway_ip:-"Not set"}" echo "Current DNS: ${dns_servers:-"Not set"}" } # Configure static IP for Ubuntu/Debian (netplan) configure_netplan() { local interface="$1" local static_ip="$2" local gateway_ip="$3" local dns_ip="$4" local netplan_file="/etc/netplan/01-netcfg.yaml" log_info "Configuring network via netplan..." # Backup existing configuration if ! backup_config "$netplan_file"; then return 1 fi # Create new netplan configuration (IPv4 and IPv6) cat < /dev/null network: version: 2 renderer: networkd ethernets: $interface: addresses: [$static_ip/24] routes: - to: default via: $gateway_ip nameservers: addresses: [$dns_ip, 8.8.8.8, 2001:4860:4860::8888] dhcp4: false dhcp6: false EOF # Validate netplan configuration if ! sudo netplan generate 2>/dev/null; then log_error "Invalid netplan configuration generated" return 1 fi # Apply configuration if sudo netplan apply; then log_success "Netplan configuration applied successfully" add_rollback_action "sudo cp ${netplan_file}.bak ${netplan_file} && sudo netplan apply" return 0 else log_error "Failed to apply netplan configuration" return 1 fi } # Configure static IP for RedHat-based systems configure_networkmanager() { local interface="$1" local static_ip="$2" local gateway_ip="$3" local dns_ip="$4" local ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-$interface" log_info "Configuring network via NetworkManager..." # Backup existing configuration if [[ -f "$ifcfg_file" ]]; then backup_config "$ifcfg_file" fi # Create new interface configuration cat < /dev/null DEVICE=$interface BOOTPROTO=none ONBOOT=yes IPADDR=$static_ip PREFIX=24 GATEWAY=$gateway_ip DNS1=$dns_ip DNS2=8.8.8.8 DEFROUTE=yes EOF # Restart NetworkManager if sudo systemctl restart NetworkManager; then log_success "NetworkManager configuration applied successfully" add_rollback_action "sudo systemctl restart NetworkManager" return 0 else log_error "Failed to restart NetworkManager" return 1 fi } # Configure static IP for Arch Linux configure_systemd_networkd() { local interface="$1" local static_ip="$2" local gateway_ip="$3" local dns_ip="$4" local network_file="/etc/systemd/network/20-$interface.network" log_info "Configuring network via systemd-networkd..." # Create network configuration cat < /dev/null [Match] Name=$interface [Network] Address=$static_ip/24 Gateway=$gateway_ip DNS=$dns_ip DNS=8.8.8.8 EOF # Enable and restart systemd-networkd sudo systemctl enable systemd-networkd if sudo systemctl restart systemd-networkd; then log_success "systemd-networkd configuration applied successfully" add_rollback_action "sudo rm -f $network_file && sudo systemctl restart systemd-networkd" return 0 else log_error "Failed to restart systemd-networkd" return 1 fi } # Test network connectivity after configuration test_network_connectivity() { local test_ip="$1" local timeout=30 local count=0 log_info "Testing network connectivity..." while [[ $count -lt $timeout ]]; do if ping -c 1 -W 3 "$test_ip" &>/dev/null; then log_success "Network connectivity test passed" return 0 fi ((count++)) echo -n "." sleep 1 done echo log_error "Network connectivity test failed after ${timeout} seconds" return 1 } # Configure SSH with enhanced security configure_ssh() { local ssh_config="/etc/ssh/sshd_config" local ssh_port="${SSH_PORT:-$DEFAULT_SSH_PORT}" log_info "Configuring SSH server..." # Backup SSH configuration if ! backup_config "$ssh_config"; then return 1 fi # Create new user if not exists if ! id "${ADMIN_USER:-$USER}" &>/dev/null; then log_info "Creating user ${ADMIN_USER:-$USER}..." sudo useradd -m -s /bin/bash "${ADMIN_USER:-$USER}" sudo usermod -aG sudo "${ADMIN_USER:-$USER}" # Set password local password=$(ask_password "Set password for user ${ADMIN_USER:-$USER}") echo "${ADMIN_USER:-$USER}:$password" | sudo chpasswd add_rollback_action "sudo userdel -r ${ADMIN_USER:-$USER}" fi # Configure SSH hardening sudo tee -a "$ssh_config" > /dev/null < /dev/null <