emq-TE1/install/move_nmconnection.sh

61 lines
2.1 KiB
Bash

#!/bin/bash
### BEGIN INIT INFO
# Provides: move_nmconnection.sh
# Required-Start: $network $local_fs $remote_fs $syslog
# Required-Stop: $network $local_fs $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Move and configure NetworkManager connections
# Description: Moves the .nmconnection files from /boot and /boot/firmware to /etc/NetworkManager/system-connections and restarts NetworkManager.
### END INIT INFO
BOOT_DIR="/boot"
FIRMWARE_DIR="/boot/firmware"
DEST_DIR="/etc/NetworkManager/system-connections"
# Verificar si hay archivos .nmconnection en /boot o /boot/firmware
if ! ls "$BOOT_DIR"/*.nmconnection &>/dev/null && ! ls "$FIRMWARE_DIR"/*.nmconnection &>/dev/null; then
echo "No se encontraron archivos .nmconnection en $BOOT_DIR ni en $FIRMWARE_DIR. Saliendo..."
exit 0
fi
get_ssid_from_file() {
grep -oP '^ssid=\K.*' "$1"
}
# Función para mover los archivos .nmconnection desde cualquier directorio
move_files() {
local source_dir="$1"
for nm_file in "$source_dir"/*.nmconnection; do
if [ -f "$nm_file" ]; then
new_ssid=$(get_ssid_from_file "$nm_file")
if [ -z "$new_ssid" ]; then
echo "El archivo $nm_file no tiene SSID, se omite."
continue
fi
existing_files=$(find "$DEST_DIR" -name "*.nmconnection" -exec grep -l "ssid=$new_ssid" {} \;)
if [ -n "$existing_files" ]; then
echo "Encontrados archivos con SSID duplicado ('$new_ssid'). Eliminando archivos antiguos..."
for file in $existing_files; do
rm -f "$file"
done
fi
mv -f "$nm_file" "$DEST_DIR/"
chmod 600 "$DEST_DIR/$(basename "$nm_file")"
fi
done
}
# Mover los archivos de ambas ubicaciones
move_files "$BOOT_DIR"
move_files "$FIRMWARE_DIR"
network_name="WiFi Connect"
nmcli connection show "$network_name" &>/dev/null && nmcli connection delete "$network_name" &>/dev/null
sudo systemctl restart NetworkManager
sudo nmcli connection reload
echo "Proceso completado."
exit 0