Update move_nmconnection.sh

This commit is contained in:
Esteban Mackay Q. 2024-12-13 12:51:25 -05:00
parent 43968a0016
commit 961f73f161
1 changed files with 30 additions and 21 deletions

View File

@ -6,15 +6,16 @@
# 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.
# 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
if ! ls "$BOOT_DIR"/*.nmconnection &>/dev/null; then
echo "No se encontraron archivos .nmconnection en $BOOT_DIR. Saliendo..."
# 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
@ -22,24 +23,32 @@ get_ssid_from_file() {
grep -oP '^ssid=\K.*' "$1"
}
for nm_file in "$BOOT_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
# 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
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
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