Add update-hosts-lxc.sh
This commit is contained in:
commit
80b9b37121
39
update-hosts-lxc.sh
Normal file
39
update-hosts-lxc.sh
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Script per La modifica del file hosts su container LXC
|
||||||
|
# Autore: Mattia Tadini
|
||||||
|
# Nome File: update-hosts-lxc.sh
|
||||||
|
# Percorso: /usr/local/lxc-scripts/
|
||||||
|
# Revisione: 1.0
|
||||||
|
|
||||||
|
# Ottieni l'indirizzo IP dell'interfaccia di rete del container
|
||||||
|
SERVER_IP=$(ip route get 8.8.8.8 | awk '{print $7}')
|
||||||
|
|
||||||
|
# Leggi il file /etc/hosts originale per trovare il nome del server
|
||||||
|
SERVER_NAME=$(awk '/^127\.0\.1\.1\s+/{print $2}' /etc/hosts)
|
||||||
|
|
||||||
|
# Controlla se SERVER_NAME è stato correttamente impostato
|
||||||
|
if [ -z "$SERVER_NAME" ]; then
|
||||||
|
echo "Errore: Nome del server non trovato nel file /etc/hosts."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Aggiorna il file /etc/hosts aggiungendo la linea specificata
|
||||||
|
echo "$SERVER_IP $SERVER_NAME" > /tmp/new_hosts
|
||||||
|
|
||||||
|
# Leggi il contenuto originale di /etc/hosts
|
||||||
|
while IFS= read -r line; do
|
||||||
|
# Ignora le righe che contengono SERVER_IP ma non SERVER_NAME
|
||||||
|
if [[ ! "$line" == *"$SERVER_IP"* ]] || [[ "$line" == *"127.0.0.1 $SERVER_NAME"* ]]; then
|
||||||
|
echo "$line" >> /tmp/new_hosts
|
||||||
|
fi
|
||||||
|
done < /etc/hosts
|
||||||
|
|
||||||
|
# Sostituisci il file /etc/hosts con la versione aggiornata
|
||||||
|
mv /tmp/new_hosts /etc/hosts
|
||||||
|
|
||||||
|
# Verifica che l'aggiunta sia stata effettuata correttamente
|
||||||
|
if grep -q "^$SERVER_IP $SERVER_NAME" /etc/hosts; then
|
||||||
|
echo "Hostname aggiunto correttamente."
|
||||||
|
else
|
||||||
|
echo "Errore nell'aggiunta del hostname."
|
||||||
|
fi
|
||||||
Loading…
Reference in New Issue
Block a user