Community discussions

MikroTik App
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Questions about generating valid random MAC?

Sat Jan 20, 2024 5:36 pm

Due to the insufficient support for Chinese characters in the v7 version of the script, and the fact that characters such as "[" and "]" are also randomly recognized as underscores, and the script is prone to crashes, I have decided to choose V6.49.10. In this version, I would like to ask the forum experts how to use the most concise script to generate a valid random MAC? For MAC addresses, it seems that the second digit can only be: 2, 6, A, E, these four characters. However, in a post by the "rextended" elder, it was explained that the second digit can only be: 0, 4, 8, C, these four characters?? May I ask, what exactly is considered a true one.
viewtopic.php?p=998248&hilit=random+mac#p956476
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Sat Jan 20, 2024 5:54 pm

:local Hex "0123456789ABCDEF" ;
:local 2st "26AE" ;
 
User avatar
vingjfg
Member Candidate
Member Candidate
Posts: 294
Joined: Fri Oct 20, 2023 1:45 pm

Re: Questions about generating valid random MAC?

Sat Jan 20, 2024 5:58 pm

Hi Rosa,

Regarding the structure of a MAC address, the 2 constraints are:
  • The LSB ("bit 0") of the first byte is 0 for a unicast address, 1 for a multicast address
  • The next bit ("bit 1") of the first byte is 0 for a globally unique address and 1 for a locally administered address.
Given that you want to generate random unicast MACs, these two bits must be "10" for unicast and locally administered as you can't guarantee they will be globally unique. This means that the lower nibble of the first byte must be in the set 0x2, 0x6, 0xA, 0xE. That aside, you can generate pretty much what you want for the higher nibble of the first byte and other 5 bytes.

When I need(ed) to generate random MAC addresses, I used to set "BEEF" as the top 2 bytes and generated the rest randomly. That made packet captures easier to search.
 
jaclaz
Long time Member
Long time Member
Posts: 669
Joined: Tue Oct 03, 2023 4:21 pm

Re: Questions about generating valid random MAC?

Sat Jan 20, 2024 6:08 pm

Locally administered unicast address vs. global unique unicast address.
0,4,8,C vs. 2,6,A,E
See:
https://developer.epages.com/blog/tech- ... addresses/
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Sun Jan 21, 2024 6:38 am

Hi Rosa,

Regarding the structure of a MAC address, the 2 constraints are:
  • The LSB ("bit 0") of the first byte is 0 for a unicast address, 1 for a multicast address
  • The next bit ("bit 1") of the first byte is 0 for a globally unique address and 1 for a locally administered address.
Given that you want to generate random unicast MACs, these two bits must be "10" for unicast and locally administered as you can't guarantee they will be globally unique. This means that the lower nibble of the first byte must be in the set 0x2, 0x6, 0xA, 0xE. That aside, you can generate pretty much what you want for the higher nibble of the first byte and other 5 bytes.

When I need(ed) to generate random MAC addresses, I used to set "BEEF" as the top 2 bytes and generated the rest randomly. That made packet captures easier to search.
Thank you for your answer. The script I am currently using is fixed 0E: 11:22:33:44:55 at the first byte, with 0E at the beginning and random generation at the end. However, I think the range is still not large enough
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Sun Jan 21, 2024 7:34 am

Locally administered unicast address vs. global unique unicast address.
0,4,8,C vs. 2,6,A,E
See:
https://developer.epages.com/blog/tech- ... addresses/
Thank you very much for your connection introduction~
[0..F][0|4|8|C] - global unique unicast
[0..F][1|5|9|D] - global unique multicast
[0..F][2|6|A|E] - locally administered unicast
[0..F][3|7|B|F] - locally administered multicast
We use multicast relatively little, and 0, 4, 8, and C are globally unique unicasts. So, if I modify the valid MAC address of VRRP myself, I should follow the rule of the second digit being 2, 6, A, and E!
 
User avatar
vingjfg
Member Candidate
Member Candidate
Posts: 294
Joined: Fri Oct 20, 2023 1:45 pm

Re: Questions about generating valid random MAC?

Sun Jan 21, 2024 10:05 am

Thank you for your answer. The script I am currently using is fixed 0E: 11:22:33:44:55 at the first byte, with 0E at the beginning and random generation at the end. However, I think the range is still not large enough
Hi Rosa,

I don't know how you generate the MAC addresses but if you feel that the range is not enough, I suspect you are duplicating one character for every value, giving you the form 0e:zz:yy:xx:ww:vv. This will result in 10^5 different MAC addresses.

We can do better ...

For example we can generate random numbers between 00 and 99 for each of the 5 bytes. This will raise the number of produced values to 100^5, or 10 billions, more than the number of people alive on earth.
{
   # Fixed prefix
    :local macAddress "0e"
    # Add the remaining 5 bytes
    for byte from=0 to=4 do={
        :local curByte ":"
        # Generate two digits. 
        :set curByte ($curByte . [:tostr [:rndnum from=0 to=9] ] )
        :set curByte ($curByte . [:tostr [:rndnum from=0 to=9] ] )
        :set macAddress ($macAddress . $curByte)
    }
    :put $macAddress
}
We can do better ...

Each byte is an 8-bit value, so instead of just picking a random number between 00 and 99, we can pick a random number between 00 and FF. This raises the values to 256^5 or 2^40, that is more than 1000 billions possible values.
{
    :local nibbleValues "0123456789abcdef"
    # Fixed prefix
    :local macAddress "0e"
    # Add the remaining 5 bytes
    for byte from=0 to=4 do={
        :local curByte ":"
        # Generate two digits. 
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set macAddress ($macAddress . $curByte)
    }
    :put $macAddress
}
We can do better ...

The first nibble is itself not fixed and can be set to any 4-bit value. The number of possible values is now 2^44, or above 16 000 billions.
{
    :local nibbleValues "0123456789abcdef"
    # First nibble is random, second nibble is fixed
    :local macAddress ([:pick $nibbleValues [:rndnum from=0 to=15] ] ."e")
    # Add the remaining 5 bytes
    for byte from=0 to=4 do={
        :local curByte ":"
        # Generate two digits. Could have done with one generation and a test
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set macAddress ($macAddress . $curByte)
    }
    :put $macAddress
}
We can do better ...

The only constraints is that the lower 2 bits of the lower order nibble of the first byte be 10. The higher 2 bits can be random, leading to the possibility to choose between 2,6, A and E. This raises the number of possible values to 2^46, or 64000 billions.
{
    :local nibbleValues "0123456789abcdef"
    # Set the first byte, random high order nibble, mostly random lower order nibble
    :local macAddress ([:pick $nibbleValues [:rndnum from=0 to=15] ])
    :set $macAddress ($macAddress . [:pick "26ae" [:rndnum from=0 to=3] ])
    # Add the remaining 5 bytes
    for byte from=0 to=4 do={
        :local curByte ":"
        # Generate two digits. Could have done with one generation and a test
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set curByte ($curByte . [:pick $nibbleValues [:rndnum from=0 to=15] ] )
        :set macAddress ($macAddress . $curByte)
    }
    :put $macAddress
}
The next step, if you want, is to use the Birthday problem/Birthday paradox to determine the probability that in a trial of n runs, you have less than 1% chance of repeating the same MAC address.
 
jaclaz
Long time Member
Long time Member
Posts: 669
Joined: Tue Oct 03, 2023 4:21 pm

Re: Questions about generating valid random MAC?

Sun Jan 21, 2024 1:41 pm

As the usual side note, not necessarily applying to this topic and/or to the actual final use of these MACs, sometimes there is confusion about the need to create a "random" ID and the need to create a "unique" ID.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 2:38 am

I have decided to choose V6.49.10. In this version, I would like to ask the forum experts how to use the most concise script to generate a valid random MAC?

Forget the endless writings of @vingjfg who doesn't read my posts,
so he doesn't know that simply 2 lines are enough to generate MAC address without complicating things too much...
{
:local rndMAC "$[:rndstr length=1 from="0123456789abcdef"]$[:rndstr length=1 from="26ae"]"
for x from=0 to=4 do={:set rndMAC "$rndMAC:$[:rndstr length=2 from="0123456789abcdef"]"}
:put $rndMAC
}
It's clear that you asked for a script that works with v6, so obviously, all features on v7 shouldn't be used.

Like this for v7
viewtopic.php?p=998248#p956476
This is for v6 except this use 2/6/A/E as requested.

random MAC code

:global rndMAC do={
    :local rnd0F do={
        :local arr0F [:toarray "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"]
        :if ($1 = 2) do={:set arr0F [:toarray "2,2,2,2,6,6,6,6,A,A,A,A,E,E,E,E"]}
        /system resource irq
        :local tmpsum 0
        :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
        :delay 00:00:00.4
        :return ($arr0F->($tmpsum % 16))
    }
    :local rnd "$[$rnd0F]$[$rnd0F 2]"
    :for x from=0 to=4 step=1 do={:set rnd "$rnd:$[$rnd0F]$[$rnd0F]"}
    :return $rnd
}

:put [$rndMAC]
Last edited by rextended on Mon Jan 22, 2024 10:56 am, edited 1 time in total.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 5:15 am

Thank you very much for your sample code~It's just V7. If [: rndnum] and [: rndstr] can be downloaded and used in V6, it would be very convenient! But I think it's unlikely~
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 5:39 am

I have decided to choose V6.49.10. In this version, I would like to ask the forum experts how to use the most concise script to generate a valid random MAC?

Forget the endless writings of @vingjfg who doesn't read my posts,
so he doesn't know that simply 2 lines are enough to generate MAC address without complicating things too much...
{
:local rndMAC "$[:rndstr length=1 from="0123456789abcdef"]$[:rndstr length=1 from="26ae"]"
for x from=0 to=4 do={:set rndMAC "$rndMAC:$[:rndstr length=2 from="0123456789abcdef"]"}
:put $rndMAC
}
It's clear that you asked for a script that works with v6, so obviously, all features on v7 shouldn't be used.

Like this for v7
viewtopic.php?p=998248#p956476
This is for v6 except this use 2/6/A/E as requested.

random MAC code

:global rndMAC do={
    :local rnd0F do={
        :local arr0F [:toarray "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"]
        :if ($1 = 2) do={:set arr0F [:toarray "2,2,2,2,6,6,6,6,A,A,A,A,E,E,E,E"]}
        /system resource irq
        :local tmpsum 0
        :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
        :return ($arr0F->($tmpsum % 16))
    }
    :local rnd "$[$rnd0F]$[$rnd0F 2]"
    :for x from=0 to=4 step=1 do={:set rnd "$rnd:$[$rnd0F]$[$rnd0F]"}
    :return $rnd
}

:put [$rndMAC]
Thank you, [rextended] God! But there are quite a few duplicate bytes in the generated MAC, and there is a high probability of 22:22:22:22:22, 66:66:66:66:66, AA: AA: AA: AA, EE: EE: EE: EE: EE: EE. I have an idea that if we could generate a random number of 0-15 and retrieve each half byte from the specified string using [: pick], would this type of duplicate byte be even fewer? Is the generated MAC also more realistic?
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 10:57 am

You use the script on x86 or CHR?

There is no random number generator on v6, and using an extremely fast CPU produces the same number.
I add one artificious delay on previous sript (please remove quoted scripts on your post) that apparently on my CHR/x86 fix the problem (and work on both v6 and v7)
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 11:08 am

You use the script on x86 or CHR?

There is no random number generator on v6, and using an extremely fast CPU produces the same number.
I add one artificious delay on previous sript (please remove quoted scripts on your post) that apparently on my CHR/x86 fix the problem (and work on both v6 and v7)
I have retested your code and the generated MAC address is indeed more realistic! May I ask the master: "Delay 00:00:00.4" refers to a delay of 4 seconds? The writing method I understand is: delay 4; What are the benefits of your writing style? May I ask for your advice? I am using X86
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 11:11 am

00:00:00.4 = 400ms

What are the benefits of your writing style?
In what sense? I'm confused.

May I ask for your advice?
This is why the forum is here, anyone can answer you.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 11:37 am

00:00:00.4 = 400ms

What are the benefits of your writing style?
In what sense? I'm confused.

May I ask for your advice?
This is why the forum is here, anyone can answer you.
Sorry~Some sentences are due to translation software issues, which may have caused you to misunderstand. Thank you again and I apologize to you! I changed it to: delay 40ms; It still works on my machine!
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?  [SOLVED]

Mon Jan 22, 2024 12:49 pm

:local t ;
:set t [$rndMAC] ;
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 2:34 pm

or directly
:local t [$rndMAC]
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Questions about generating valid random MAC?

Mon Jan 22, 2024 3:52 pm

or directly
:local t [$rndMAC]
Well, I've read your post and understood it~Thank you, great god~

Who is online

Users browsing this forum: Bing [Bot] and 4 guests