#!/bin/bash # Script to set fixed MAC address and disable Wi-Fi if eth0 configuration succeeds # Must be run with sudo! # --- Configuration --- ETH0_MAC="00:00:00:00:00:00" # Default MAC - will be changed by setup script # 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 } # --- Execution --- # Set MAC for eth0 and disable Wi-Fi if successful if set_mac "eth0" "$ETH0_MAC"; then # 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