-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptipfixe
46 lines (34 loc) · 1.32 KB
/
scriptipfixe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# Détectez l'interface réseau principale
INTERFACE=$(ip -4 route list default | awk '{print $5}')
# Si aucune interface n'est trouvée, quittez le script
if [[ -z $INTERFACE ]]; then
echo "Aucune interface réseau principale trouvée."
exit 1
fi
# Obtenez l'adresse IP actuelle
CURRENT_IP=$(ip -4 addr show $INTERFACE | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
# Obtenez le masque de sous-réseau actuel
NETMASK=$(ip -4 addr show $INTERFACE | grep -oP '(?<=inet\s)\d+(\.\d+){3}/\d+' | cut -d'/' -f2)
# Obtenez la passerelle par défaut
GATEWAY=$(ip route | grep default | awk '{print $3}')
# Sauvegarde du fichier de configuration actuel
cp /etc/network/interfaces /etc/network/interfaces.backup
# Écrivez la nouvelle configuration
cat <<EOL > /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto $INTERFACE
iface $INTERFACE inet static
address $CURRENT_IP
netmask $NETMASK
gateway $GATEWAY
EOL
# Redémarrez le réseau pour appliquer les modifications
/etc/init.d/networking restart
echo "Adresse IP statique configurée avec succès pour l'interface $INTERFACE."