From ed23bc34615febd96ff57feaab88685543932032 Mon Sep 17 00:00:00 2001 From: Tiago Date: Sun, 15 Feb 2026 11:23:25 +0000 Subject: [PATCH] Update debian/set_mac_wifi.sh --- debian/set_mac_wifi.sh | 99 +++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 29 deletions(-) diff --git a/debian/set_mac_wifi.sh b/debian/set_mac_wifi.sh index 38fb408..9e41036 100644 --- a/debian/set_mac_wifi.sh +++ b/debian/set_mac_wifi.sh @@ -53,7 +53,7 @@ set_mac() { setup_udev_persistence() { local interface="$1" local target_mac="$2" - + local original_mac original_mac=$(ip link show "$interface" | awk '/ether/ {print $2}' | tr '[:upper:]' '[:lower:]') target_mac=$(echo "$target_mac" | tr '[:upper:]' '[:lower:]') @@ -63,11 +63,29 @@ setup_udev_persistence() { return 1 fi - # Cria o ficheiro de regras - echo "ACTION==\"add\", SUBSYSTEM==\"net\", ATTR{address}==\"$original_mac\", RUN+=\"/usr/bin/ip link set dev \$name address $target_mac\"" > "$UDEV_RULES_FILE" - - udevadm control --reload-rules - log "Regra Udev criada/atualizada em $UDEV_RULES_FILE." + # Cria o conteúdo da regra + local rule_content="ACTION==\"add\", SUBSYSTEM==\"net\", ATTR{address}==\"$original_mac\", RUN+=\"/usr/bin/ip link set dev \$name address $target_mac\"" + + # Escreve no ficheiro de regras + echo "$rule_content" > "$UDEV_RULES_FILE" + + # Verifica se o ficheiro foi criado com sucesso + if [ -f "$UDEV_RULES_FILE" ]; then + log "Sucesso: Ficheiro de regras Udev criado em $UDEV_RULES_FILE." + log "Conteúdo da regra: $rule_content" + else + log "Erro: Falha ao criar o ficheiro de regras Udev em $UDEV_RULES_FILE." + return 1 + fi + + # Recarrega as regras do udev + if udevadm control --reload-rules; then + log "Regras Udev recarregadas com sucesso." + else + log "Erro: Falha ao recarregar regras Udev." + return 1 + fi + return 0 } @@ -144,36 +162,59 @@ fi # 3. Determinar o MAC Address TARGET_MAC="" +# Função para perguntar confirmação ao utilizador +ask_confirmation() { + local message="$1" + local default="$2" # "y" or "n" + + while true; do + if [ "$default" = "y" ]; then + read -p "$message [Y/n]: " choice < /dev/tty + else + read -p "$message [y/N]: " choice < /dev/tty + fi + + # Default choice if user just presses Enter + if [ -z "$choice" ]; then + choice="$default" + fi + + case "$choice" in + y|Y|yes|Yes|YES) + return 0 + ;; + n|N|no|No|NO) + return 1 + ;; + *) + echo "Por favor, responda sim ou não." + ;; + esac + done +} + # Verificar argumento ($1) if [ -n "$1" ]; then TARGET_MAC="$1" log "MAC definido via argumento: $TARGET_MAC" else # Verificar se é interativo - # IMPORTANTE: Ler de /dev/tty para funcionar com curl | bash if [ -t 0 ] && [ -e /dev/tty ]; then - # Abre /dev/tty para ler input do utilizador - exec < /dev/tty - - echo "Nenhum MAC definido. Deseja:" - echo "1) Gerar um novo MAC único automático" - echo "2) Introduzir um MAC manualmente" - read -p "Opção [1/2]: " choice - - case "$choice" in - 1) - TARGET_MAC=$(generate_unique_mac) - log "MAC gerado automaticamente: $TARGET_MAC" - ;; - 2) - read -p "Introduza o MAC (formato XX:XX:XX:XX:XX:XX): " TARGET_MAC - ;; - *) - log "Opção inválida." - exit 1 - ;; - esac - # Restaura stdin se necessário (embora geralmente não seja crítico aqui) + # Pergunta ao utilizador se quer gerar um MAC automaticamente + if ask_confirmation "Deseja gerar um novo MAC único automaticamente?" "y"; then + TARGET_MAC=$(generate_unique_mac) + log "MAC gerado automaticamente: $TARGET_MAC" + else + # Pergunta para introduzir MAC manualmente + while true; do + read -p "Introduza o MAC (formato XX:XX:XX:XX:XX:XX): " TARGET_MAC < /dev/tty + if [[ "$TARGET_MAC" =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]]; then + break + else + echo "Formato de MAC inválido. Por favor, introduza no formato XX:XX:XX:XX:XX:XX." + fi + done + fi else # Modo não interativo (boot/curl) sem argumentos: Gerar automático log "Execução não interativa. A gerar MAC único..."