#!/bin/bash # Importer les fonctions communes source "$(dirname "$0")/../common/common_utils.sh" # Vérifier si le script est exécuté en root check_root # Vérifie si une IP est fournie if [ -z "$1" ]; then echo "Usage: $0 " exit 1 fi IP="$1" echo "🔍 Recherche du nom d'hôte pour l'IP : $IP" # Fonction pour vérifier si une commande est disponible 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 [ -n "$HOSTNAME_LLMNR" ]; then echo "✅ LLMNR: $HOSTNAME_LLMNR" exit 0 fi else echo "⚠ LLMNR non disponible (resolvectl absent)" 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 [ -n "$HOSTNAME_MDNS" ]; then echo "✅ mDNS (Avahi): $HOSTNAME_MDNS" exit 0 fi else echo "⚠ mDNS non disponible (avahi-resolve absent)" fi # 3️⃣ Vérification du cache ARP if command_exists arp; then HOSTNAME_ARP=$(arp -a | grep "$IP" | awk '{print $1}') if [ -n "$HOSTNAME_ARP" ]; then echo "✅ ARP: $HOSTNAME_ARP" exit 0 fi else echo "⚠ ARP non disponible" 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 [ -n "$HOSTNAME_NETBIOS" ]; then echo "✅ NetBIOS: $HOSTNAME_NETBIOS" exit 0 fi else echo "⚠ NetBIOS non disponible (nmblookup absent)" fi # 5️⃣ Scan réseau avec Nmap pour identifier l'hôte if command_exists 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 else echo "⚠ Nmap non disponible" fi echo "❌ Aucune correspondance trouvée." exit 1