amélioration de la gestion du DNS sur la machine

This commit is contained in:
Cédric Abonnel 2025-03-13 08:46:43 +01:00
parent 428110dad1
commit 0cfc306351
2 changed files with 38 additions and 6 deletions

View File

@ -21,10 +21,32 @@ command_exists() {
command -v "$1" &> /dev/null
}
# Détection du serveur DNS en prenant en compte dnsmasq et fallback sur resolv.conf
get_dns_server() {
local dns_server=""
if command_exists resolvectl; then
dns_server=$(resolvectl dns | awk '/^Global:/ {print $2; exit}')
fi
if [ -z "$dns_server" ] && command_exists dnsmasq; then
dns_server=$(ss -tulpn | grep dnsmasq | awk '{print $5}' | cut -d: -f1 | head -n1)
fi
if [ -z "$dns_server" ] && [ -f /etc/resolv.conf ]; then
dns_server=$(grep -m1 '^nameserver' /etc/resolv.conf | awk '{print $2}')
fi
echo "${dns_server:-9.9.9.9}" # Utilisation d'un fallback 9.9.9.9 si aucun DNS trouvé
}
DNS_SERVER=$(get_dns_server)
echo "🛜 Serveur DNS détecté : $DNS_SERVER"
# 1⃣ Test LLMNR (Link-Local Multicast Name Resolution)
if command_exists resolvectl; then
HOSTNAME_LLMNR=$(resolvectl query "$IP" 2>/dev/null | grep -oP '(?<=Name:\s).*' | head -n1)
if [ ! -z "$HOSTNAME_LLMNR" ]; then
if [ -n "$HOSTNAME_LLMNR" ]; then
echo "✅ LLMNR: $HOSTNAME_LLMNR"
exit 0
fi
@ -35,7 +57,7 @@ fi
# 2⃣ Test mDNS (Multicast DNS / Avahi)
if command_exists avahi-resolve; then
HOSTNAME_MDNS=$(avahi-resolve -a "$IP" 2>/dev/null | awk '{print $2}')
if [ ! -z "$HOSTNAME_MDNS" ]; then
if [ -n "$HOSTNAME_MDNS" ]; then
echo "✅ mDNS (Avahi): $HOSTNAME_MDNS"
exit 0
fi
@ -46,7 +68,7 @@ fi
# 3⃣ Vérification du cache ARP
if command_exists arp; then
HOSTNAME_ARP=$(arp -a | grep "$IP" | awk '{print $1}')
if [ ! -z "$HOSTNAME_ARP" ]; then
if [ -n "$HOSTNAME_ARP" ]; then
echo "✅ ARP: $HOSTNAME_ARP"
exit 0
fi
@ -57,7 +79,7 @@ fi
# 4⃣ Vérification via NetBIOS (Windows/Samba)
if command_exists nmblookup; then
HOSTNAME_NETBIOS=$(nmblookup -A "$IP" 2>/dev/null | grep -m1 '<00>' | awk '{print $1}')
if [ ! -z "$HOSTNAME_NETBIOS" ]; then
if [ -n "$HOSTNAME_NETBIOS" ]; then
echo "✅ NetBIOS: $HOSTNAME_NETBIOS"
exit 0
fi
@ -67,8 +89,8 @@ fi
# 5⃣ Scan réseau avec Nmap pour identifier l'hôte
if command_exists nmap; then
HOSTNAME_NMAP=$(nmap -sP "$IP" 2>/dev/null | grep "Nmap scan report" | awk '{print $5}')
if [ ! -z "$HOSTNAME_NMAP" ]; then
HOSTNAME_NMAP=$(nmap --dns-servers "$DNS_SERVER" -sP "$IP" 2>/dev/null | grep "Nmap scan report" | awk '{print $5}')
if [ -n "$HOSTNAME_NMAP" ]; then
echo "✅ Nmap: $HOSTNAME_NMAP"
exit 0
fi

View File

@ -146,6 +146,16 @@ EOF
# Redémarrer dnsmasq pour appliquer les modifications
systemctl restart dnsmasq
# Modifier /etc/resolv.conf
echo 'nameserver 127.0.0.1' > /etc/resolv.conf
echo "Configuration DNS mise à jour sur 127.0.0.1"
# Redémarrer les services nécessaires
echo "Redémarrage des services..."
systemctl restart dnsmasq 2>/dev/null && echo "dnsmasq redémarré."
systemctl restart systemd-resolved 2>/dev/null && echo "systemd-resolved redémarré."
systemctl restart networking 2>/dev/null && echo "networking redémarré."
systemctl restart NetworkManager 2>/dev/null && echo "NetworkManager redémarré."
echo "Configuration terminée avec succès!"
echo "Serveur DHCP/DNS fonctionnel sur $IP_SERVEUR"
echo "Les clients recevront une IP entre $DHCP_START et $DHCP_END et seront associés au domaine $DOMAINE"