From bc34b7aece84f5ee5ed17986089f0f06bb00959b Mon Sep 17 00:00:00 2001 From: Tiago Date: Sun, 15 Feb 2026 11:28:19 +0000 Subject: [PATCH] Update debian/set_mac_wifi.sh --- debian/set_mac_wifi.sh | 70 ++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/debian/set_mac_wifi.sh b/debian/set_mac_wifi.sh index 9e41036..1758539 100644 --- a/debian/set_mac_wifi.sh +++ b/debian/set_mac_wifi.sh @@ -13,11 +13,42 @@ log() { echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" | tee -a "$LOG_FILE" } +# 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 +} + # Função para gerar MAC único baseado no Serial do CPU generate_unique_mac() { local serial serial=$(awk '/Serial/ {print $3}' /proc/cpuinfo | tr -d ' ') - + if [ -z "$serial" ]; then printf '%02X:%02X:%02X:%02X:%02X:%02X' $((0x02 | (RANDOM % 256) & 0xFE)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) else @@ -31,7 +62,7 @@ generate_unique_mac() { set_mac() { local interface="$1" local mac="$2" - + if ! ip link show "$interface" &> /dev/null; then log "Interface $interface não encontrada em set_mac." return 1 @@ -92,7 +123,6 @@ setup_udev_persistence() { # Função robusta para verificar persistência is_persistence_configured() { if [ -f "$UDEV_RULES_FILE" ]; then - # Verifica se o ficheiro não está vazio (proteção extra) if [ -s "$UDEV_RULES_FILE" ]; then log "Ficheiro de persistência encontrado: $UDEV_RULES_FILE" return 0 @@ -144,7 +174,6 @@ if is_persistence_configured; then fi # 2. Esperar pela Interface de Rede (Importante para boot/curl) -# O script pode correr antes da interface estar pronta. IFACE="eth0" log "A aguardar pela interface $IFACE..." for i in {1..30}; do @@ -162,37 +191,6 @@ 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" @@ -236,7 +234,7 @@ if set_mac "$IFACE" "$TARGET_MAC"; then setup_udev_persistence "$IFACE" "$TARGET_MAC" else log "Aviso: Falha ao definir MAC em tempo real, mas a tentar criar persistência..." - setup_udev_persistence "$IFACE" "$TARGET_MAC" + setup_udev_persistence "$IFACE" "$TARGET_MAC fi # 5. Lançar Monitor de Rede