Mac address (last 4-digits) into WiFi SSID, automatically

I want to config mac address last 4-digits into WiFi SSID.
I found the new wifi-scripts file is mac80211.uc, some related part:

set ${si}=wifi-iface
set ${si}.device='${name}'
set ${si}.network='lan'
set ${si}.mode='ap'
set ${si}.ssid='${defaults?.ssid || "OpenWrt"}'        <====== here, I need it likes "OpenWrt-xxxx" according mac address
set ${si}.encryption='${defaults?.encryption || "none"}'
set ${si}.key='${defaults?.key || ""}'

how to do it, pls help or any other suggestions, thanks.

I would patch /etc/board.d/02_network for your device/target
E.g. ath79
https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=target/linux/ath79/generic/base-files/etc/board.d/02_network;hb=93a48afe96444b3f9f2468b959399aecacb2cbb2

Take the detected mac of your choice (wan, lan, label, whatever), manipulate the string to get the format you desire, then call ucidef_set_wireless with appropriate parameters.

https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=package/base-files/files/lib/functions/uci-defaults.sh;hb=5b8c385f90c6e7f0216d95550bd1399903ca84c1#l657

searching forum for "random MAC" gets you pretty far, even if you don't want random :slight_smile:

Mac address as Wifi SSID - #6 by juppin plus Random MAC but it should have first 10 characters same for 2 SSIDs - #6 by JustAnotherEndUser.

1 Like

I'v tried the code:

cat /sys/class/net/wlan*/address | head -n 1 | tr -d ':' | tr -d '\n' | tail -c 4

It's a shell cmd, I do not know how to combined the line into the ucode file. This is the new v24.10 problem to me. On openwrt v23.05 before, I did it:

Thanks. You attached links cannot open.

I don't have any OpenWRT device to try it on, but this works "on paper" on a vanilla Linux box, and should work if added to rc.local, plus a network/wifi restart:

uci set wireless.@wifi-iface[0].ssid="OpenWrt-"$(cat /sys/class/net/wlan*/address| head -n 1 | tr -d ":" | tr -d "\n"| tail -c 4)

it's pretty much the same as the one you already linked to, command wise.

lantis1008's solution is prettier, because it'll only run once, but will probably get wiped during every upgrade, and I'm guessing the one you liked to will be too.

@lantis1008 you need to use the URL link button for the deep links to work, it's a recent forum feature.

1 Like

If you these are shell variables then you have to use surrounding double quotes and not single quotes...

Urgh. What a garbage feature.
There goes me ever linking anything helpful again.

1 Like

Hi, @frollic @_bernd , this bash cmd:

cat /sys/class/ieee80211/phy0/macaddress | head -n 1 | tr -d ":" | tr -d "\n"| tail -c 4 | tr a-z A-Z

worked on ssh terminal.

but I tried many combinations into the mac80211.uc file, not pass!!, likes:

set ${si}.ssid='${defaults?.ssid || "OpenWrt-" + $(cat /sys/class/ieee80211/phy0/macaddress | head -n 1 | tr -d ":" | tr -d "\n"| tail -c 4 | tr a-z A-Z)}'

You need to use the full path into /sys/... like the one in my example.

Sorry, I always use the full path. corrected in above post

I don't know where you put this. But again: Are you sure your command gets executed because they are surrounded by SINGLE quotes which normally prevents VARIABLE EXPANTION.

And just to ask this dumb question: Why can't you set your SSID "statically"?

Hi, in the file mac80211.uc, wifi mac address has been captured to variable macaddr:

let macaddr = trim(readfile(`/sys/class/ieee80211/${phy_name}/macaddress`));

Then, mod that wifi ssid line as:

set ${si}.ssid='${defaults?.ssid || "OpenWrt-" + macaddr}'

I got the wifi ssid with a FULL mac address:

OpenWrt-d4:33:38:e1:ab:cd

Now the question is
How to take the "last 4-digits" and make it to Capital letters to get "OpenWrt-ABCD".

Because I have 3 or more router/APs using Openwrt FW, I need set it in different ssid automatically.

1 Like

The mesh11sd package does this autonomously by default.
You can find the code here:
mesh11sd

You could use mesh11sd to do it for you, even if your aps are cabled, but that would probably be overkill.

Simplified, the code constructs an "SSID" string based on the default value in the config file:

  1. Uses UCI to get the default SSID
  2. Uses the ip utility to get the mac address of the bridge (br-lan)
  3. Gets the last 4 digits of the mac address as a suffix string, using awk.
  4. Uses UCI to get the wireless band (2g 5g etc), but you have not mentioned this so you would not bother.
  5. Concatenates default SSID with Band and suffix and does;
    echo "set $ap.ssid='$ssid-$band-$suffix'" | uci batch where $ap identifies the wireless interface, would be something like wireless.default_radio1
  6. In your version you would do uci commit wireless

In your case you would create a set of script commands and put them into the " Script to run on first boot (uci-defaults)" text box in the Firmware Selector and generate the firmware for the APs.

On first run, the /etc/config/wireless file will be updated with the required suffix based on the mac address, as you require.

2 Likes

last 4-digits from label mac address if exist or eth0 mac address.

source /lib/functions/system.sh
MAC_LABEL=$(get_mac_label)
MAC_ETH0=$(cat /sys/class/net/eth0/address 2>/dev/null)
MACADDR=${MAC_LABEL:-${MAC_ETH0}}
DEVICE_ID="$(macaddr_geteui $MACADDR | cut -c 3-6| tr 'a-z' 'A-Z')" 

DEFAULTS_SSID='your_SSID'
set ${si}.ssid="${DEFAULTS_SSID:-OpenWrt}-${DEVICE_ID}"

Thanks, I'll check this.

Thanks, The WiFi Mac address is already in the variable macaddr in the file mac80211.uc.

Hi, all,
Following my direction, I have got the right result OpenWrt-ABCD by using:

set ${si}.ssid='${defaults?.ssid || "OpenWrt-" + uc(substr(macaddr, 12, 2) + substr(macaddr, 15, 2))}'

here, function uc(string) is for "change to UpperCase".

3 Likes

Hi, @frollic , you're right, put such configs mod into rc.local bash file is a good practice to avoid touching the openwrt wifi source code.

The following code tested works on my router:

# /package/base-files/files/etc/rc.local

uci set wireless.default_radio0.ssid=OpenWrt-$(cat /sys/class/ieee80211/phy0/macaddress | head -n 1 | tr -d ":" | tr -d "\n" | tail -c 4 | tr a-z A-Z)
uci set wireless.default_radio0.encryption=psk2
uci set wireless.default_radio0.wds=1
uci set wireless.default_radio0.key=12345678
uci commit wireless
wifi reload
/etc/init.d/network reload

Thanks

2 Likes