Community discussions

MikroTik App
 
barara
just joined
Topic Author
Posts: 11
Joined: Tue Jan 12, 2016 9:41 am

Routeros 7 Interface random Mac address

Sun Sep 11, 2022 11:13 am

Hello
I'm not familiar with mikrotik scripts. Please understand even the low level questions.
In the picture below, I want to randomly select the hexadecimal characters of number 1 and combine them with the randomly generated hexadecimal string of number 2 and finally make it like number 5.
Is there a way?
Thank you.
random mac.png
You do not have the required permissions to view the files attached to this post.
Last edited by barara on Sun Sep 11, 2022 2:01 pm, edited 1 time in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Routeros 7 Interface random Mac address

Sun Sep 11, 2022 12:15 pm

Why?

Where is part 1 (6 first hex of mac) coming from?
Part 2 are the 6 last hex of you random mac
 
barara
just joined
Topic Author
Posts: 11
Joined: Tue Jan 12, 2016 9:41 am

Re: Routeros 7 Interface random Mac address

Sun Sep 11, 2022 1:29 pm

Why?

Where is part 1 (6 first hex of mac) coming from?
Part 2 are the 6 last hex of you random mac
Thanks for reply.
The dhcp server connected to the wan port did not assign an IP address for a specific MAC address.
So, I used the method of combining the specified values.
It seems that IP is assigned based on the oui value.
part 1 hex values are pre-specified plain text.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Routeros 7 Interface random Mac address

Sun Sep 11, 2022 5:38 pm

Here you go
{
:local array { "c8:ea:f8"; "70:9f:2d"; "38:3b:26"; "5c:fa:fb"; "9c:7b:ef" }   
:local part1 ($array->[:rndnum from=0 to=([:len $array]-1)])

:local hex1 [:rndstr length=6 from="0123456789abcdef"]
:local part2 ([:pick $hex1 0 2].":".[:pick $hex1 2 4].":".[:pick $hex1 4 6])

:local Mac ($part1.":".$part2)
:put $Mac
}
Just add more mac to the array to your need.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Routeros 7 Interface random Mac address  [SOLVED]

Mon Sep 12, 2022 1:11 am

":" are not needed, and uppercase and lowercase are ignored (but must be a valic MAC: the 2nd digit of 1st HEX must be 0, 4, 8 or C)
:put "$[:rndstr length=1 from="0123456789ABCDEF"]$[:rndstr length=1 from="048C"]$[:rndstr length=10 from="0123456789ABCDEF"]"
 
barara
just joined
Topic Author
Posts: 11
Joined: Tue Jan 12, 2016 9:41 am

Re: Routeros 7 Interface random Mac address

Mon Sep 12, 2022 7:38 am

Thanks to both of you for the replies.
the script works very well.
 
juanfrancisco2023
just joined
Posts: 1
Joined: Mon Apr 24, 2023 11:40 pm
Location: Toledo Norte de santander

Re: Routeros 7 Interface random Mac address

Mon Apr 24, 2023 11:45 pm

":" are not needed, and uppercase and lowercase are ignored (but must be a valic MAC: the 2nd digit of 1st HEX must be 0, 4, 8 or C)
:put "$[:rndstr length=1 from="0123456789ABCDEF"]$[:rndstr length=1 from="048C"]$[:rndstr length=10 from="0123456789ABCDEF"]"
rndstr como se utiliza en la v6.48? no existe ese comando
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Routeros 7 Interface random Mac address

Tue Apr 25, 2023 11:12 am

Generate 12 times a random number
viewtopic.php?f=9&t=175453&p=858629#p858629
each time you do % 16 you can have random results from 0 to 15, pick from one arraythe corresponding digit.

Make sure that the 1st bit (bit 0) on 2nd digit is always 0, or the network do not work as expected....
and if is generated MAC must have 2nd bit (bit 1) on 2nd digit always 1.
:global randomnum do={
    /system resource irq
    :local tmpsum 0
    :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
    :set   tmpsum [:tostr $tmpsum]
    :local lentmp [:len   $tmpsum]
    :return [:tonum [:pick $tmpsum ($lentmp - 2) $lentmp]]
}

{
:local arrhex [:toarray "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"]
:local rndmac ; :local tmp
:for x from=1 to=12 step=1 do={
   :set tmp ([$randomnum] % 16)
   # this makes it always a valid MAC
   :if ($x =  2) do={:set tmp (($tmp | 0x2) & 0xE)}
   :set rndmac "$rndmac$($arrhex->$tmp)"
   :if ([:tostr [:len $rndmac]] ~ "(2|5|8|11|14)") do={:set rndmac "$rndmac:"}
}
:put $rndmac
}

Who is online

Users browsing this forum: mhenriques and 19 guests