#!/bin/bash

# Function to generate a random MAC address
generate_mac() {
    printf '00:%02X:%02X:%02X:%02X:%02X\n' \
        $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256))
}

# Check if correct number of arguments is provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <start_vmid> <end_vmid>"
    exit 1
fi

start_vmid=$1
end_vmid=$2

# Loop through the range and change MAC address for each LXC container
for ((vmid=start_vmid; vmid<=end_vmid; vmid++)); do
    new_mac=$(generate_mac)
    echo "Setting MAC address for CT $vmid to $new_mac..."
    
    # Update the container config file
    if [ -f /etc/pve/lxc/$vmid.conf ]; then
        sed -i "/^net0/ s/,hwaddr=[^,]*/,hwaddr=$new_mac/" /etc/pve/lxc/$vmid.conf
        echo "MAC address updated for CT $vmid."
    else
        echo "Config file for CT $vmid not found. Skipping."
    fi

done

echo "MAC address update process completed."
