Files
scripts/debian/set_mac_wifi.sh
T
2026-02-11 22:03:06 +00:00

113 lines
3.7 KiB
Bash

#!/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
if ip link show "$interface" &> /dev/null; then
# Try to set MAC
if ip link set "$interface" down && \
ip link set dev "$interface" address "$mac" && \
ip link set "$interface" up; then
echo "$(date): MAC address for $interface set to $mac" | tee -a /var/log/set_mac.log
success=true
else
echo "$(date): Failed to set MAC address for $interface" | tee -a /var/log/set_mac.log
fi
else
echo "$(date): Interface $interface not found" | tee -a /var/log/set_mac.log
fi
# Return success (0) or failure (1)
$success
}
# 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