42 lines
1.3 KiB
Bash
42 lines
1.3 KiB
Bash
#!/bin/bash
|
|
# apply_network.sh — Configure le réseau via NetworkManager
|
|
# Usage: apply_network.sh dhcp
|
|
# ou: apply_network.sh static <ip/prefix> <gateway> <dns>
|
|
# Exemple: apply_network.sh static 192.168.1.100/24 192.168.1.1 8.8.8.8,1.1.1.1
|
|
|
|
set -e
|
|
|
|
MODE="$1"
|
|
CON="Wired connection 1"
|
|
|
|
# Trouve la connexion active si le nom par défaut a changé
|
|
if ! nmcli con show "$CON" &>/dev/null; then
|
|
CON=$(nmcli -t -f NAME,TYPE con show --active | grep ethernet | cut -d: -f1 | head -1)
|
|
fi
|
|
|
|
[[ -z "$CON" ]] && echo "Aucune connexion Ethernet trouvée." && exit 1
|
|
|
|
if [[ "$MODE" == "dhcp" ]]; then
|
|
nmcli con mod "$CON" ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns ""
|
|
nmcli con up "$CON"
|
|
echo "OK: mode DHCP appliqué."
|
|
|
|
elif [[ "$MODE" == "static" ]]; then
|
|
IP_PREFIX="$2" # ex: 192.168.1.100/24
|
|
GATEWAY="$3" # ex: 192.168.1.1
|
|
DNS="$4" # ex: 8.8.8.8,1.1.1.1
|
|
|
|
[[ -z "$IP_PREFIX" || -z "$GATEWAY" ]] && echo "IP et gateway requis." && exit 1
|
|
|
|
nmcli con mod "$CON" \
|
|
ipv4.method manual \
|
|
ipv4.addresses "$IP_PREFIX" \
|
|
ipv4.gateway "$GATEWAY" \
|
|
ipv4.dns "${DNS:-8.8.8.8,1.1.1.1}"
|
|
nmcli con up "$CON"
|
|
echo "OK: IP statique $IP_PREFIX appliquée."
|
|
|
|
else
|
|
echo "Usage: $0 dhcp | static <ip/prefix> <gateway> [dns]" && exit 1
|
|
fi
|