Nom du fichier : ScanReseauLocal.sh
#!/bin/bash
# Si on n'est pas dans un terminal, se relancer dans mate-terminal
if ! [ -t 1 ]; then
mate-terminal -- bash -c "sudo bash '$0'; exec bash"
exit
fi
# =============================================================
# ScanReseauLocal.sh — Scanner réseau local (Debian)
# Dépendances : nmap → sudo apt install nmap
# =============================================================
# Utilisation :
# Scan automatique (détecte ton réseau tout seul)
# sudo ~/Bureau/ScanReseauLocal.sh
# Scan sur une plage personnalisée
# sudo ~/Bureau/ScanReseauLocal.sh 192.168.1.0/24
# Scan + ports ouverts d'un appareil précis
# sudo ~/Bureau/ScanReseauLocal.sh --ports 192.168.0.42
set -uo pipefail
# ── Couleurs ─────────────────────────────────────────────────
BOLD='\033[1m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
ORANGE='\033[38;5;214m'
RED='\033[0;31m'
NC='\033[0m'
# ── Vérifications préalables ─────────────────────────────────
check_deps() {
local missing=()
for cmd in nmap ip; do
command -v "$cmd" &>/dev/null || missing+=("$cmd")
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo -e "${RED}Dépendances manquantes : ${missing[*]}${NC}"
echo -e "Installe-les avec : ${ORANGE}sudo apt install nmap iproute2${NC}"
exit 1
fi
}
# ── Détection automatique du réseau actif ────────────────────
detect_network() {
local cidr
cidr=$(ip -4 addr show scope global \
| grep -oP '(?<=inet\s)\d+\.\d+\.\d+\.\d+/\d+' \
| head -1)
if [[ -z "$cidr" ]]; then
echo -e "${RED}Impossible de détecter le réseau. Vérif ta connexion.${NC}"
exit 1
fi
echo "$cidr"
}
# ── Scan principal ───────────────────────────────────────────
scan_network() {
local cidr="$1"
echo
echo -e "${BOLD}${CYAN}══════════════════════════════════════════${NC}"
echo -e "${BOLD}${CYAN} 🔍 SCAN RÉSEAU LOCAL ${NC}"
echo -e "${BOLD}${CYAN}══════════════════════════════════════════${NC}"
echo -e " Plage cible : ${ORANGE}${cidr}${NC}"
echo -e " Démarré le : $(date '+%d/%m/%Y %H:%M:%S')"
echo -e "${BOLD}${CYAN}══════════════════════════════════════════${NC}"
echo
local raw
raw=$(sudo nmap -sn -T4 \
-PE -PP \
-PS21,22,23,25,80,443,445,3389,8080 \
-PA80,443 \
-PR \
"$cidr" 2>/dev/null)
local count=0
while IFS= read -r line; do
if [[ "$line" =~ ^"Nmap scan report for " ]]; then
local host="${line#Nmap scan report for }"
local ip hostname
local regex='\(([^)]+)\)$'
if [[ "$host" =~ $regex ]]; then
hostname="${host%% (*}"
ip="${BASH_REMATCH[1]}"
else
ip="$host"
hostname=""
fi
local mac vendor mac_line
mac_line=$(echo "$raw" | grep -A3 "^Nmap scan report for ${host}$" \
| grep "MAC Address" || true)
if [[ -n "$mac_line" ]]; then
mac=$(echo "$mac_line" | awk '{print $3}')
vendor=$(echo "$mac_line" | sed 's/.*(\(.*\))/\1/')
else
mac="N/A (machine locale ou accès limité)"
vendor=""
fi
count=$(( count + 1 ))
echo -e " ${BOLD}${GREEN}▸ Appareil #${count}${NC}"
echo -e " IP : ${ORANGE}${ip}${NC}"
[[ -n "$hostname" ]] && \
echo -e " Hostname : ${CYAN}${hostname}${NC}"
echo -e " MAC : ${mac}"
[[ -n "$vendor" ]] && \
echo -e " Fabricant: ${vendor}"
echo
fi
done <<< "$raw"
echo -e "${BOLD}${CYAN}══════════════════════════════════════════${NC}"
echo -e " ${BOLD}${GREEN}${count} appareil(s) détecté(s)${NC}"
echo -e "${BOLD}${CYAN}══════════════════════════════════════════${NC}"
echo
}
# ── Optionnel : scan de ports sur un appareil ────────────────
scan_ports() {
local ip="$1"
echo
echo -e "${BOLD}${CYAN}── Scan des ports ouverts sur ${ip} ─────────${NC}"
sudo nmap -T4 --open --top-ports 100 "$ip" 2>/dev/null \
| grep -E "^[0-9]+/(tcp|udp)|Nmap scan" \
| sed "s/^/ /"
echo
}
# ── Point d'entrée ───────────────────────────────────────────
main() {
check_deps
local cidr="${1:-}"
local port_ip=""
while [[ $# -gt 0 ]]; do
case "$1" in
--ports)
port_ip="${2:-}"
shift 2
;;
*)
cidr="$1"
shift
;;
esac
done
[[ -z "$cidr" ]] && cidr=$(detect_network)
scan_network "$cidr"
if [[ -n "$port_ip" ]]; then
scan_ports "$port_ip"
fi
}
main "$@"
