Files
scripts/debian/set_mac_wifi.sh
T

193 lines
7.3 KiB
Bash

#!/bin/bash
# Script to set fixed MAC address and disable Wi-Fi if eth0 configuration succeeds
# Must be run with sudo!
# Update: Agora gera um MAC único automaticamente se nenhum for especificado (resolver problema do DM9601).
# --- Configuration ---
ETH0_MAC="00:00:00:00:00:00" # Default MAC - será alterado automaticamente se mantiver este valor
# WLAN0_MAC="XX:XX:XX:XX:XX:XX" # Uncomment and set if needed
# Verify root privileges
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
# --- Functions ---
# Function to set MAC and verify success
set_mac() {
local interface="$1"
local mac="$2"
local success=false
local log_file="/var/log/set_mac.log"
# Verifica se a interface existe
if ! ip link show "$interface" &> /dev/null; then
echo "$(date): Interface $interface não encontrada." | tee -a "$log_file"
return 1
fi
# Verifica se macchanger está instalado
if command -v macchanger >/dev/null 2>&1; then
echo "$(date): Usando macchanger para definir MAC de $interface para $mac" | tee -a "$log_file"
# Tenta usar macchanger
if ip link set "$interface" down && \
macchanger -m "$mac" "$interface" >/dev/null 2>&1 && \
ip link set "$interface" up; then
echo "$(date): Sucesso ao definir MAC com macchanger para $interface (novo: $mac)" | tee -a "$log_file"
success=true
else
echo "$(date): Falha ao definir MAC com macchanger para $interface. Tentando fallback com ip link..." | tee -a "$log_file"
fi
fi
# Fallback para ip link (se macchanger não estiver disponível ou falhar)
if [ "$success" = false ]; then
# Tenta com ip link diretamente
if ip link set "$interface" down && \
ip link set dev "$interface" address "$mac" && \
ip link set "$interface" up; then
echo "$(date): Sucesso ao definir MAC com 'ip link' para $interface (novo: $mac)" | tee -a "$log_file"
success=true
else
echo "$(date): Falha ao definir MAC para $interface com ambos os métodos." | tee -a "$log_file"
return 1
fi
fi
# Retorna sucesso (0) ou falha (1)
return $([ "$success" = true ] && echo 0 || echo 1)
}
# Function to configure static IP
configure_static_ip() {
local ip_script="/usr/local/bin/set_static_ip.sh"
if [ -f "$ip_script" ]; then
# Get current IP configuration
local interface
if interface=$(get_active_interface); then
local current_ip=$(ip -4 addr show "$interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [[ "$current_ip" =~ ^169\.254\. ]]; then
# Link-local address, likely no DHCP
log "No valid IP address found for $interface, configuring static IP..."
IP_ADDRESS_ENV="10.10.0.100/24" bash "$ip_script"
fi
fi
fi
}
# Function to check if interface has IP address
has_ip_address() {
local interface="$1"
# Check if interface is UP and has an IPv4 address
if ip link show "$interface" | grep -q "state UP" && \
ip -4 addr show "$interface" | grep -q "inet "; then
return 0
else
return 1
fi
}
# Function to disable Wi-Fi
disable_wifi() {
# Create log directory if it doesn't exist
mkdir -p /var/log
# Wait for 1 minute (60 seconds) before disabling Wi-Fi
echo "$(date): Waiting 60 seconds before checking eth0 IP and disabling Wi-Fi..." | tee -a /var/log/set_mac.log
sleep 60
# Check if eth0 has an IP address before disabling Wi-Fi
if has_ip_address "eth0"; then
if ip link show "wlan0" &> /dev/null; then
if ip link set "wlan0" down; then
echo "$(date): Wi-Fi (wlan0) disabled successfully (eth0 has IP)" | tee -a /var/log/set_mac.log
else
echo "$(date): Failed to disable Wi-Fi (wlan0)" | tee -a /var/log/set_mac.log
fi
else
echo "$(date): wlan0 interface not found (Wi-Fi already disabled?)" | tee -a /var/log/set_mac.log
fi
else
echo "$(date): eth0 does not have an IP address. Wi-Fi remains active." | tee -a /var/log/set_mac.log
fi
}
# --- Novas Funções Complementares (Dinâmicas) ---
# Função para gerar um MAC único baseado no Serial do Raspberry Pi
# Garante que cada dispositivo fica com um MAC diferente mas consistente
generate_unique_mac() {
local serial
serial=$(awk '/Serial/ {print $3}' /proc/cpuinfo | tr -d ' ')
if [ -z "$serial" ]; then
# Fallback aleatório caso não encontre serial
printf '%02X:%02X:%02X:%02X:%02X:%02X' $((0x02 | (RANDOM % 256) & 0xFE)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256))
else
# Gera hash do serial para criar os octetos
local hash
hash=$(echo -n "$serial-dm9601" | md5sum | cut -c1-10)
# 02 define como Local Admin Address
echo "02:${hash:0:2}:${hash:2:2}:${hash:4:2}:${hash:6:2}:${hash:8:2}"
fi
}
# Função para criar regra Udev persistente (Best Practice)
# Isto faz com que o MAC seja aplicado no boot, evitando erros de "Device Busy"
setup_udev_persistence() {
local interface="$1"
local target_mac="$2"
local log_file="/var/log/set_mac.log"
# Obtém o MAC original (físico) do adaptador
local original_mac
original_mac=$(ip link show "$interface" | awk '/ether/ {print $2}' | tr '[:upper:]' '[:lower:]')
target_mac=$(echo "$target_mac" | tr '[:upper:]' '[:lower:]')
if [ -z "$original_mac" ]; then return 1; fi
local rules_file="/etc/udev/rules.d/81-mac-spoof.rules"
# Se a regra já existe para este MAC original, não faz nada
if grep -q "$original_mac" "$rules_file" 2>/dev/null; then
return 0
fi
echo "$(date): Criando regra udev persistente para $interface" | tee -a "$log_file"
echo "ACTION==\"add\", SUBSYSTEM==\"net\", ATTR{address}==\"$original_mac\", RUN+=\"/usr/bin/ip link set dev \$name address $target_mac\"" >> "$rules_file"
udevadm control --reload-rules
}
# --- Execução Principal ---
# LÓGICA COMPLEMENTAR:
# Se ETH0_MAC for o valor padrão (00:00...) ou vazio, gera um novo automaticamente.
if [ -z "$ETH0_MAC" ] || [ "$ETH0_MAC" == "00:00:00:00:00:00" ]; then
echo "$(date): Nenhum MAC específico definido. Gerando MAC único automático..." | tee -a /var/log/set_mac.log
ETH0_MAC=$(generate_unique_mac)
echo "$(date): Novo MAC gerado: $ETH0_MAC" | tee -a /var/log/set_mac.log
fi
# Executa a lógica original com o MAC definido (seja o do config ou o gerado)
if set_mac "eth0" "$ETH0_MAC"; then
# Se definiu o MAC com sucesso, tenta criar persistência via Udev
# Isto resolve o problema do adapter mantendo o MAC em futuros boots
setup_udev_persistence "eth0" "$ETH0_MAC"
# Check if disable_wifi is already running
if ! pgrep -f "disable_wifi" > /dev/null; then
#configure_static_ip
disable_wifi &
else
echo "$(date): disable_wifi is already running" | tee -a /var/log/set_mac.log
fi
else
echo "$(date): Could not set MAC for eth0. Wi-Fi remains active" | tee -a /var/log/set_mac.log
fi
# Uncomment if you want to set MAC for wlan0 (optional)
# if [[ -n "$WLAN0_MAC" ]]; then
# set_mac "wlan0" "$WLAN0_MAC"
# fi