mirror of https://gitlab.com/hp3icc/emq-TE1.git
70 lines
2.4 KiB
Bash
70 lines
2.4 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 to /etc/NetworkManager/system-connections and restarts NetworkManager.
|
|
### END INIT INFO
|
|
|
|
# Directorio de la partición boot
|
|
BOOT_DIR="/boot"
|
|
|
|
# Directorio de destino para los archivos de configuración de NetworkManager
|
|
DEST_DIR="/etc/NetworkManager/system-connections"
|
|
|
|
# Función para obtener el SSID del archivo .nmconnection
|
|
get_ssid_from_file() {
|
|
grep -oP '^ssid=\K.*' "$1"
|
|
}
|
|
|
|
# Buscar archivos .nmconnection en /boot y moverlos a /etc/NetworkManager/system-connections/
|
|
for nm_file in $BOOT_DIR/*.nmconnection; do
|
|
if [ -f "$nm_file" ]; then
|
|
# Obtener el SSID del archivo nuevo
|
|
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
|
|
|
|
# Buscar archivos existentes en /etc/NetworkManager/system-connections que tengan el mismo SSID
|
|
existing_files=$(find "$DEST_DIR" -name "*.nmconnection" -exec grep -l "ssid=$new_ssid" {} \;)
|
|
|
|
# Si se encuentran archivos con el mismo SSID, eliminarlos
|
|
if [ -n "$existing_files" ]; then
|
|
echo "Encontrados archivos con SSID duplicado ('$new_ssid'). Eliminando archivos antiguos..."
|
|
for file in $existing_files; do
|
|
existing_ssid=$(get_ssid_from_file "$file")
|
|
|
|
if [ "$existing_ssid" == "$new_ssid" ]; then
|
|
echo "Eliminando archivo antiguo: $file con SSID '$existing_ssid'"
|
|
rm -f "$file"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Mover el archivo .nmconnection a la carpeta de configuración de NetworkManager (forzando la sobrescritura)
|
|
mv -f "$nm_file" "$DEST_DIR/"
|
|
|
|
# Asegurarse de que los permisos sean correctos (solo lectura para root)
|
|
chmod 600 "$DEST_DIR/$(basename $nm_file)"
|
|
fi
|
|
done
|
|
|
|
network_name="WiFi Connect"
|
|
nmcli connection show "$network_name" &>/dev/null && nmcli connection delete "$network_name" &>/dev/null
|
|
# Detener NetworkManager
|
|
sudo systemctl stop NetworkManager
|
|
|
|
# Iniciar NetworkManager
|
|
sudo systemctl start NetworkManager
|
|
|
|
# Recargar las configuraciones de NetworkManager para que se reconozcan los cambios
|
|
sudo nmcli connection reload
|
|
|
|
echo "Proceso completado."
|