#!/usr/bin/env bash set -euo pipefail # Linux Static IP Configurator # Supports: # - NetworkManager (nmcli) # - Netplan # # Run as root: # sudo ./set-static-ip.sh if [[ "${EUID}" -ne 0 ]]; then echo "Dieses Script muss als root ausgeführt werden." echo "Bitte nutze: sudo $0" exit 1 fi command_exists() { command -v "$1" >/dev/null 2>&1 } pause() { echo read -r -p "Drücke Enter, um fortzufahren..." } get_interfaces() { ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo$' } select_interface() { echo "Verfügbare Netzwerkinterfaces:" echo mapfile -t interfaces < <(get_interfaces) if [[ "${#interfaces[@]}" -eq 0 ]]; then echo "Keine Netzwerkinterfaces gefunden." exit 1 fi for i in "${!interfaces[@]}"; do state=$(cat "/sys/class/net/${interfaces[$i]}/operstate" 2>/dev/null || echo "unknown") echo " $((i + 1))) ${interfaces[$i]} [$state]" done echo while true; do read -r -p "Interface auswählen [1-${#interfaces[@]}]: " choice if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#interfaces[@]} )); then INTERFACE="${interfaces[$((choice - 1))]}" break fi echo "Ungültige Auswahl." done } validate_ipv4() { local ip="$1" local IFS=. local -a octets read -ra octets <<< "$ip" [[ "${#octets[@]}" -eq 4 ]] || return 1 for octet in "${octets[@]}"; do [[ "$octet" =~ ^[0-9]+$ ]] || return 1 (( octet >= 0 && octet <= 255 )) || return 1 done return 0 } validate_cidr() { local cidr="$1" [[ "$cidr" =~ ^[0-9]+$ ]] && (( cidr >= 0 && cidr <= 32 )) } ask_network_data() { echo echo "Statische IPv4-Konfiguration" echo "----------------------------" while true; do read -r -p "IPv4-Adresse (z.B. 192.168.1.50): " IP_ADDRESS validate_ipv4 "$IP_ADDRESS" && break echo "Ungültige IPv4-Adresse." done while true; do read -r -p "CIDR-Präfix (z.B. 24): " PREFIX validate_cidr "$PREFIX" && break echo "Ungültiges CIDR-Präfix." done while true; do read -r -p "Gateway (z.B. 192.168.1.1): " GATEWAY validate_ipv4 "$GATEWAY" && break echo "Ungültiges Gateway." done read -r -p "DNS-Server, mit Leerzeichen getrennt [1.1.1.1 8.8.8.8]: " DNS_INPUT DNS_INPUT="${DNS_INPUT:-1.1.1.1 8.8.8.8}" DNS_SERVERS=() for dns in $DNS_INPUT; do if ! validate_ipv4 "$dns"; then echo "Ungültiger DNS-Server: $dns" exit 1 fi DNS_SERVERS+=("$dns") done echo echo "Gewählte Konfiguration:" echo " Interface : $INTERFACE" echo " IP-Adresse: $IP_ADDRESS/$PREFIX" echo " Gateway : $GATEWAY" echo " DNS : ${DNS_SERVERS[*]}" echo read -r -p "Konfiguration anwenden? [j/N]: " confirm case "${confirm,,}" in j|ja|y|yes) ;; *) echo "Abgebrochen." exit 0 ;; esac } configure_networkmanager() { echo echo "NetworkManager erkannt." local connection connection=$(nmcli -t -f NAME,DEVICE connection show --active \ | awk -F: -v iface="$INTERFACE" '$2 == iface {print $1; exit}') if [[ -z "$connection" ]]; then connection=$(nmcli -t -f NAME,DEVICE connection show \ | awk -F: -v iface="$INTERFACE" '$2 == iface {print $1; exit}') fi if [[ -z "$connection" ]]; then connection="static-$INTERFACE" echo "Keine bestehende Verbindung gefunden." echo "Erstelle NetworkManager-Verbindung: $connection" nmcli connection add \ type ethernet \ ifname "$INTERFACE" \ con-name "$connection" else echo "Verwende Verbindung: $connection" fi local backup_dir="/root/network-backup-$(date +%Y%m%d-%H%M%S)" mkdir -p "$backup_dir" if [[ -d /etc/NetworkManager/system-connections ]]; then cp -a /etc/NetworkManager/system-connections "$backup_dir/" 2>/dev/null || true fi echo "Backup: $backup_dir" local dns_csv dns_csv=$(IFS=,; echo "${DNS_SERVERS[*]}") nmcli connection modify "$connection" \ ipv4.method manual \ ipv4.addresses "$IP_ADDRESS/$PREFIX" \ ipv4.gateway "$GATEWAY" \ ipv4.dns "$dns_csv" \ ipv4.ignore-auto-dns yes nmcli connection up "$connection" echo echo "Statische IP wurde mit NetworkManager gesetzt." } configure_netplan() { echo echo "Netplan erkannt." local backup_dir="/root/netplan-backup-$(date +%Y%m%d-%H%M%S)" mkdir -p "$backup_dir" cp -a /etc/netplan/. "$backup_dir/" 2>/dev/null || true echo "Backup: $backup_dir" local config_file="/etc/netplan/99-static-ip.yaml" local dns_yaml="" for dns in "${DNS_SERVERS[@]}"; do if [[ -n "$dns_yaml" ]]; then dns_yaml+=", " fi dns_yaml+="$dns" done cat > "$config_file" </dev/null 2>&1; then configure_networkmanager elif [[ -d /etc/netplan ]] && compgen -G "/etc/netplan/*.yaml" >/dev/null; then configure_netplan elif [[ -d /etc/netplan ]] && command_exists netplan; then configure_netplan else echo echo "Kein unterstütztes Netzwerk-Backend erkannt." echo echo "Unterstützt werden:" echo " - NetworkManager / nmcli" echo " - Netplan" echo echo "Bei Debian mit /etc/network/interfaces ist eine manuelle" echo "Konfiguration erforderlich." exit 1 fi show_result