ajout de find_hostname.sh

This commit is contained in:
Cédric Abonnel 2025-03-13 08:29:33 +01:00
parent f5c7214107
commit d6d9f5b14a
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,74 @@
#!/bin/bash
# Vérifie si une IP est fournie
if [ -z "$1" ]; then
echo "Usage: $0 <IP>"
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
}
# 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
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 [ ! -z "$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 [ ! -z "$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 [ ! -z "$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 -sP "$IP" 2>/dev/null | grep "Nmap scan report" | awk '{print $5}')
if [ ! -z "$HOSTNAME_NMAP" ]; then
echo "✅ Nmap: $HOSTNAME_NMAP"
exit 0
fi
else
echo "⚠ Nmap non disponible"
fi
echo "❌ Aucune correspondance trouvée."
exit 1

View File

@ -10,6 +10,9 @@ check_root
if ! command -v dnsmasq &> /dev/null; then
echo "Installation de dnsmasq..."
apt update && apt install -y dnsmasq
echo "Installation d'outils réseau..."
apt install -y systemd-resolved avahi-utils net-tools smbclient nmap
systemctl disable --now avahi-daemon
else
echo "dnsmasq est déjà installé."
fi