From 80b9b371212ff7e7e3775bb27121c4ccd70724cd Mon Sep 17 00:00:00 2001 From: Mattia Tadini Date: Tue, 14 Jan 2025 16:39:00 +0000 Subject: [PATCH] Add update-hosts-lxc.sh --- update-hosts-lxc.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 update-hosts-lxc.sh diff --git a/update-hosts-lxc.sh b/update-hosts-lxc.sh new file mode 100644 index 0000000..44c7144 --- /dev/null +++ b/update-hosts-lxc.sh @@ -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 \ No newline at end of file