#!/bin/bash # # Réinitialise le hotspot "2Limited" sur la carte WiFi disponible. # Utilisation : sudo ./reset_hotspot.sh # set -e # stop en cas d'erreur # --- Configuration --- SSID="2Limited" PASS="calais62" CHANNEL=6 CONN_NAME="2Limited" # --- Détection de l'interface WiFi --- detect_wifi() { # Liste des interfaces WiFi gérées par NetworkManager (hors loopback, ethernet, etc.) local iface=$(nmcli -t -f TYPE,DEVICE device | grep -E '^wifi:' | head -1 | cut -d: -f2) if [ -z "$iface" ]; then echo "❌ Aucune interface WiFi détectée." exit 1 fi echo "$iface" } # --- Nettoyage de l'ancienne connexion --- clean_connection() { if nmcli con show "$CONN_NAME" &>/dev/null; then echo "🔁 Suppression de l'ancienne connexion '$CONN_NAME'..." sudo nmcli con delete "$CONN_NAME" >/dev/null 2>&1 || true fi } # --- Création du hotspot --- create_hotspot() { local iface="$1" echo "📡 Création du hotspot sur $iface..." sudo nmcli con add type wifi ifname "$iface" mode ap con-name "$CONN_NAME" ssid "$SSID" >/dev/null sudo nmcli con mod "$CONN_NAME" 802-11-wireless.mode ap sudo nmcli con mod "$CONN_NAME" 802-11-wireless.band bg sudo nmcli con mod "$CONN_NAME" 802-11-wireless.channel "$CHANNEL" sudo nmcli con mod "$CONN_NAME" wifi-sec.key-mgmt wpa-psk sudo nmcli con mod "$CONN_NAME" wifi-sec.psk "$PASS" sudo nmcli con mod "$CONN_NAME" 802-11-wireless.powersave 2 sudo nmcli con mod "$CONN_NAME" ipv4.method shared sudo nmcli con mod "$CONN_NAME" connection.autoconnect yes } # --- Démarrage du hotspot --- start_hotspot() { echo "▶️ Démarrage du hotspot..." sudo nmcli con up "$CONN_NAME" >/dev/null if [ $? -eq 0 ]; then echo "✅ Hotspot '$SSID' actif." # Afficher l'IP de la passerelle (le PC) local gateway=$(ip -4 addr show "$(nmcli -t -f DEVICE device status | grep -E '^wlan' | cut -d: -f1)" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1) echo "🌐 IP du PC (passerelle) : $gateway" echo "🔑 Mot de passe : $PASS" echo "📶 Canal : $CHANNEL" else echo "❌ Échec du démarrage du hotspot." exit 1 fi } # --- Exécution --- main() { echo "🔄 Réinitialisation du hotspot '$SSID'..." WIFI_IFACE=$(detect_wifi) echo "🔌 Interface détectée : $WIFI_IFACE" clean_connection create_hotspot "$WIFI_IFACE" start_hotspot echo "" echo "✨ Prêt ! L'ESP peut maintenant se connecter à '$SSID'." } # Vérification des droits if [ "$EUID" -ne 0 ]; then echo "⚠️ Ce script nécessite les droits root. Relance avec sudo." exit 1 fi main