Need help with MAC randomisation

Hi.

I just need to make my MAC random on each reboot. I am using GL-Inet Mudi (v2).

After some searching I found this script but it is not looking as trustworthy as it:

#!/bin/sh

# Update package list and install macchanger
opkg update
opkg install macchanger

# Create the MAC randomization script
cat << 'EOF' > /etc/init.d/randomize_mac
#!/bin/sh /etc/rc.common

START=99

start() {
    for iface in $(ip link show | grep -E '^[0-9]+:' | cut -d ':' -f 2 | cut -d ' ' -f 2); do
        if [ "$iface" != "lo" ]; then
            macchanger -r $iface
        fi
    done
}
EOF

# Make the MAC randomization script executable
chmod +x /etc/init.d/randomize_mac

# Enable the MAC randomization script
/etc/init.d/randomize_mac enable

# Delete this script
rm -- "$0"

Can anyone help me with this? Is this working? Or maybe need to be fixed?

Will this one work?:

#!/bin/sh

INSTALL_PATH="/etc/init.d/randomize_mac"

echo "Creating MAC randomizer script..."
cat << 'EOF' > $INSTALL_PATH
#!/bin/sh /etc/rc.common

START=99

start() {
    generate_random_mac() {
        echo $(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))
    }

    change_mac() {
        local iface=$1
        local new_mac=$(generate_random_mac)
        ip link set dev $iface down
        ip link set dev $iface address $new_mac
        ip link set dev $iface up
    }

    for iface in $(ip link show | grep -E '^[0-9]+:' | cut -d ':' -f 2 | cut -d ' ' -f 2); do
        if [ "$iface" != "lo" ]; then
            change_mac $iface
        fi
    done
}
EOF

echo "Making MAC randomizer script executable..."
chmod +x $INSTALL_PATH
echo "Success!"

echo "Enabling MAC randomizer script to run at boot time..."
/etc/init.d/randomize_mac enable
echo "Success!"

echo "Deleting installation script..."
rm -- "$0"

I am a little bit newbie so please don’t be strict:)

I don’t want to use any of external apps not to overfill my router’s memory.

Here is example of output of this command:

echo $(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%256)))

Output:


localhost:~# echo $(printf '%02x' $((RANDOM%256)))
":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%0
2x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%2
56)))":"$(printf '%02x' $((RANDOM%256)))":"$(print
f '%02x' $((RANDOM%256)))
a7:03:f2:fa:45:5d
localhost:~# echo $(printf '%02x' $((RANDOM%256)))
":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%0
2x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%2
56)))":"$(printf '%02x' $((RANDOM%256)))":"$(print
f '%02x' $((RANDOM%256)))
f5:3f:8b:6e:f9:84
localhost:~# echo $(printf '%02x' $((RANDOM%256)))
":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%0
2x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%2
56)))":"$(printf '%02x' $((RANDOM%256)))":"$(print
f '%02x' $((RANDOM%256)))
e8:db:17:37:29:fd
localhost:~# echo $(printf '%02x' $((RANDOM%256)))
":"$(printf '%02x' $((RANDOM%256)))":"$(printf '%0
2x' $((RANDOM%256)))":"$(printf '%02x' $((RANDOM%2
56)))":"$(printf '%02x' $((RANDOM%256)))":"$(print
f '%02x' $((RANDOM%256)))
15:e4:80:fc:35:ee

That looks right to me

Check also:

https://forum.archive.openwrt.org/viewtopic.php?id=44716
https://forum.archive.openwrt.org/viewtopic.php?id=48090

My variant:

uci set wireless.@wifi-iface[0].macaddr=hexdump -n1 -e'1/1 "%02x"' /dev/random | head -c2``hexdump -n5 -e'5/1 ":%02x"' /dev/random && uci commit wireless