v1.9.3: Fix wifi_connect (escaping shell + URL) + log dédié
Bugs corrigés: - launcher.php passait SSID/PASS au shell sans escapeshellarg(): un mot de passe avec $/&/;/espace cassait silencieusement la commande avant que nmcli ne soit appelé. Cause probable des retours clients "ça bloque au cliquer sur Se connecter". - wifi.html n'encodait pas SSID/PASS dans l'URL: caractères &/+/= corrompaient la requête. Observabilité: - Nouveau fichier logs/wifi_connect.log avec timestamps stricts - launcher.php log la requête entrante (IP, longueurs SSID/PASS) - connexion.sh: fonction log_wc(), snapshots NM avant/après, capture stdout+stderr nmcli, code retour explicite, fallback SSID dérivé du serial si deviceName indisponible. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
113
connexion.sh
113
connexion.sh
@@ -1,51 +1,92 @@
|
||||
#!/bin/bash
|
||||
echo "-------"
|
||||
echo "Start connexion shell script at $(date)"
|
||||
# Connect wlan0 to a client WiFi network, replacing the local hotspot.
|
||||
# Called by launcher.php (wifi_connect action) with: $1=SSID $2=PASSWORD
|
||||
# All output is appended to logs/wifi_connect.log by the caller's redirect,
|
||||
# but we also write timestamped lines here to make the log self-contained.
|
||||
|
||||
# Get deviceName from database for hotspot SSID
|
||||
DEVICE_NAME=$(sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "SELECT value FROM config_table WHERE key='deviceName'")
|
||||
echo "Device Name: $DEVICE_NAME"
|
||||
WIFI_LOG="/var/www/nebuleair_pro_4g/logs/wifi_connect.log"
|
||||
DB="/var/www/nebuleair_pro_4g/sqlite/sensors.db"
|
||||
|
||||
# Find and disable any active hotspot connection
|
||||
echo "Disable Hotspot..."
|
||||
# Get all wireless connections that are currently active (excludes the target WiFi)
|
||||
ACTIVE_HOTSPOT=$(nmcli -t -f NAME,TYPE,DEVICE connection show --active | grep ':802-11-wireless:wlan0' | cut -d: -f1)
|
||||
log_wc() {
|
||||
local ts
|
||||
ts=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "[$ts] [connexion.sh] $*"
|
||||
}
|
||||
|
||||
if [ -n "$ACTIVE_HOTSPOT" ]; then
|
||||
echo "Disabling hotspot connection: $ACTIVE_HOTSPOT"
|
||||
sudo nmcli connection down "$ACTIVE_HOTSPOT"
|
||||
else
|
||||
echo "No active hotspot found"
|
||||
log_wc "================================================================"
|
||||
log_wc "=== connexion.sh started ==="
|
||||
log_wc "PID=$$ user=$(whoami)"
|
||||
log_wc "SSID='$1' (len=${#1})"
|
||||
log_wc "PASS=[HIDDEN] (len=${#2})"
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
log_wc "ERROR: empty SSID, aborting"
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$2" ]; then
|
||||
log_wc "ERROR: empty PASSWORD, aborting"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get deviceName for the hotspot fallback SSID (with busy timeout against lock)
|
||||
DEVICE_NAME=$(sqlite3 -cmd ".timeout 5000" "$DB" "SELECT value FROM config_table WHERE key='deviceName'")
|
||||
log_wc "deviceName from DB: '$DEVICE_NAME'"
|
||||
|
||||
# Fallback SSID derived from RPi serial if DB read failed or value is empty
|
||||
if [ -z "$DEVICE_NAME" ]; then
|
||||
serial_number=$(cat /proc/cpuinfo | grep Serial | awk '{print substr($3, length($3) - 7)}')
|
||||
DEVICE_NAME="nebuleair-pro-$serial_number"
|
||||
log_wc "WARN: deviceName empty in DB, fallback to '$DEVICE_NAME'"
|
||||
fi
|
||||
|
||||
# Snapshot NM state BEFORE we touch anything (helps debug)
|
||||
log_wc "--- NetworkManager state BEFORE ---"
|
||||
log_wc "wlan0 state: $(nmcli -g GENERAL.STATE device show wlan0 2>&1)"
|
||||
log_wc "wlan0 connection: $(nmcli -g GENERAL.CONNECTION device show wlan0 2>&1)"
|
||||
log_wc "active connections:"
|
||||
nmcli -t -f NAME,TYPE,DEVICE connection show --active 2>&1 | while read -r line; do log_wc " $line"; done
|
||||
|
||||
# Find and bring down any active wireless connection on wlan0 (the hotspot)
|
||||
ACTIVE_HOTSPOT=$(nmcli -t -f NAME,TYPE,DEVICE connection show --active | grep ':802-11-wireless:wlan0' | cut -d: -f1)
|
||||
if [ -n "$ACTIVE_HOTSPOT" ]; then
|
||||
log_wc "Disabling active wireless connection on wlan0: '$ACTIVE_HOTSPOT'"
|
||||
DOWN_OUT=$(sudo nmcli connection down "$ACTIVE_HOTSPOT" 2>&1)
|
||||
DOWN_RC=$?
|
||||
log_wc "nmcli connection down rc=$DOWN_RC out: $DOWN_OUT"
|
||||
else
|
||||
log_wc "No active wireless connection on wlan0 (nothing to bring down)"
|
||||
fi
|
||||
|
||||
log_wc "Sleeping 5s to let NM release wlan0..."
|
||||
sleep 5
|
||||
|
||||
echo "Start connection with:"
|
||||
echo "SSID: $1"
|
||||
echo "Password: [HIDDEN]"
|
||||
sudo nmcli device wifi connect "$1" password "$2"
|
||||
log_wc "--- Attempting nmcli wifi connect ---"
|
||||
log_wc "Command: sudo nmcli device wifi connect '$1' password [HIDDEN]"
|
||||
NMCLI_OUT=$(sudo nmcli device wifi connect "$1" password "$2" 2>&1)
|
||||
NMCLI_RC=$?
|
||||
log_wc "nmcli exit code: $NMCLI_RC"
|
||||
log_wc "nmcli output: $NMCLI_OUT"
|
||||
|
||||
# Check if connection is successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Connection to $1 is successful"
|
||||
# Snapshot NM state AFTER attempt
|
||||
log_wc "--- NetworkManager state AFTER ---"
|
||||
log_wc "wlan0 state: $(nmcli -g GENERAL.STATE device show wlan0 2>&1)"
|
||||
log_wc "wlan0 connection: $(nmcli -g GENERAL.CONNECTION device show wlan0 2>&1)"
|
||||
|
||||
# Update SQLite to reflect connected status
|
||||
sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='connected' WHERE key='WIFI_status'"
|
||||
echo "Updated database: WIFI_status = connected"
|
||||
if [ $NMCLI_RC -eq 0 ]; then
|
||||
log_wc "SUCCESS: connected to '$1'"
|
||||
sqlite3 -cmd ".timeout 5000" "$DB" "UPDATE config_table SET value='connected' WHERE key='WIFI_status'"
|
||||
log_wc "DB updated: WIFI_status = connected"
|
||||
else
|
||||
echo "Connection to $1 failed"
|
||||
echo "Restarting hotspot..."
|
||||
log_wc "FAILURE: connection to '$1' failed (rc=$NMCLI_RC), restarting hotspot..."
|
||||
|
||||
# Recreate hotspot with current deviceName as SSID
|
||||
sudo nmcli device wifi hotspot ifname wlan0 ssid "$DEVICE_NAME" password nebuleaircfg
|
||||
# Recreate hotspot with deviceName (or fallback) as SSID
|
||||
HS_OUT=$(sudo nmcli device wifi hotspot ifname wlan0 ssid "$DEVICE_NAME" password nebuleaircfg 2>&1)
|
||||
HS_RC=$?
|
||||
log_wc "nmcli hotspot rc=$HS_RC out: $HS_OUT"
|
||||
|
||||
# Update SQLite to reflect hotspot mode
|
||||
sqlite3 /var/www/nebuleair_pro_4g/sqlite/sensors.db "UPDATE config_table SET value='hotspot' WHERE key='WIFI_status'"
|
||||
echo "Updated database: WIFI_status = hotspot"
|
||||
echo "Hotspot restarted with SSID: $DEVICE_NAME"
|
||||
sqlite3 -cmd ".timeout 5000" "$DB" "UPDATE config_table SET value='hotspot' WHERE key='WIFI_status'"
|
||||
log_wc "DB updated: WIFI_status = hotspot"
|
||||
log_wc "Hotspot restarted with SSID: '$DEVICE_NAME'"
|
||||
fi
|
||||
|
||||
echo "End connexion shell script"
|
||||
echo "-------"
|
||||
|
||||
|
||||
log_wc "=== connexion.sh end ==="
|
||||
|
||||
Reference in New Issue
Block a user