aredn/files/etc/uci-defaults/94_fix_mac_addresses

42 lines
1.3 KiB
Bash

#!/bin/sh
# Fix mac addresses as necessary
fix=""
eth0=$(ifconfig eth0 2> /dev/null | grep HWaddr | sed s/^.*HWaddr\ // | sed s/\ //g)
wlan0=$(ifconfig wlan0 2> /dev/null | grep HWaddr | sed s/^.*HWaddr\ // | sed s/\ //g)
brlan=$(ifconfig br-lan 2> /dev/null | grep HWaddr | sed s/^.*HWaddr\ // | sed s/\ //g)
boardid=$(/usr/local/bin/get_boardid)
mfg=$(/usr/local/bin/get_hardware_mfg)
if [ "$eth0" = "$wlan0" -a "$eth0" != "" ]; then
fix="lan wan dtdlink"
elif [ "$wlan0" = "" ]; then
fix="lan wan dtdlink"
elif [ "$brlan" = "00:03:7F:11:23:C6" ]; then # Fix AR300M
fix="lan"
elif [ "$mfg" = "MikroTik" ]; then # Fix all MikroTiks
fix="lan wan dtdlink"
fi
mkdir -p /etc/aredn_include
list=""
for i in $fix
do
if [ ! -s /etc/aredn_include/fixedmac.$i ]; then
# Some mac addresses are stored in /etc/board.json
mac=$(jsonfilter -i /etc/board.json -e @.network.$i.macaddr 2> /dev/null | awk '{print toupper($0)}')
if [[ "$list" =~ "#$mac#" ]]; then
# Dont allow reuse
mac=""
fi
if [ "$mac" = "" ]; then
# Generate a safe mac address
mac=$(hexdump -n5 -e'/5 "02" 5/1 ":%02X"' /dev/random)
fi
echo -e "\toption macaddr $mac" > /etc/aredn_include/fixedmac.$i
list="$list#$mac#"
fi
done