Generate random mac addresses via batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Generate random mac addresses via batch?

#1 Post by noprogrammer » 08 Aug 2013 11:35

Hi batch gurus,

I found this amazing one liner for Unix shell for fully randomizing MAC addresses:
MAC=`echo -n 00; hexdump -n 5 -v -e '/1 ":%02X"' /dev/urandom;`
It works like a charm... :)

I managed to change the MAC address with a cmd script but I'd like to generate bogus (formally correct) addresses to apply whenever needed!

I do know that in Windows I'm confined to %RANDOM%... maybe there's something better available?

Also, even if I manage to generate appropriate decimal numbers how do I calculate hex values from them? Or is there an executable to mimic hexdump's behaviour in the snippet above?

What would you do? Any ideas? :?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Generate random mac addresses via batch?

#2 Post by penpen » 08 Aug 2013 13:02

It is not a oneliner, as there is no command like hexdump, but i think something like this is what you are looking for:

Code: Select all

@echo off
cls
call :intToHex hex0 "(%random%<<15)+%random%"
call :intToHex hex1 "(%random%<<15)+%random%"
set "MAC=00:%hex1:~-4,2%:%hex1:~-2,2%:%hex0:~-6,2%:%hex0:~-4,2%:%hex0:~-2,2%"
echo MAC=%MAC%
goto :eof

:intToHex
::   %~1 variable to store the hex value
::   %~2 int32 string value
   setlocal enableDelayedExpansion
   set /A "num=%~2"
   set "hex="
   set "digits=0123456789ABCDEF"
   for /L %%a in (0,1,7) do (
      set /A "nibble=num&0xF", "num>>=4"
      for %%b in (!nibble!) do set "hex=!digits:~%%b,1!!hex!"
   )
   endlocal & set "%~1=%hex%"
   goto :eof

penpen

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Generate random mac addresses via batch?

#3 Post by noprogrammer » 09 Aug 2013 06:39

Wow, great, see me deeply impressed! :o

I don't really get what you're doing in the sub but hey, it seems to be the counterpart of the unix snippet!
Thank you very much!

Just a final thought: I know that vendors use specific ranges for their products.
Gigabyte for instance use 90-2B-34.. on their onboard LAN controllers.

Does that mean, that generated bogus addresses like
00-0C-67-36-AA-0D
00-3B-69-3C-44-8C
00-40-4F-28-AD-69
may be detected as being invalid?

Just wondering if using one of these could lead to "errors" sooner or later... ?

Thanks for your suggestions! 8)

aGerman
Expert
Posts: 4731
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generate random mac addresses via batch?

#4 Post by aGerman » 09 Aug 2013 10:09

I don't know much about the validity of MAC addresses but since 00 is a valid hex byte value I guess it should be valid for MAC addresses as well.

Here's another technique that calculates all octets randomly:

Code: Select all

setlocal EnableDelayedExpansion
set /a "i1 = 0, i2 = 0" &for /l %%i in (0,1,23) do set /a "i1 = (i1 << 1) | (!random! & 1), i2 = (i2 << 1) | (!random! & 1)"
cmd /c exit /b %i1%
set "mac=%=exitcode:~2%
cmd /c exit /b %i2%
set "mac=%mac%%=exitcode:~2%
endlocal &set "mac=%mac:~,2%-%mac:~2,2%-%mac:~4,2%-%mac:~6,2%-%mac:~8,2%-%mac:~-2%"

echo %mac%


Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Generate random mac addresses via batch?

#5 Post by penpen » 09 Aug 2013 15:03

noprogrammer wrote:(...)
Does that mean, that generated bogus addresses like
00-0C-67-36-AA-0D
00-3B-69-3C-44-8C
00-40-4F-28-AD-69
may be detected as being invalid?

Of course it may be detected, but as far as i know in most cases there are no autodetecting units in internet, as this costs performance.
But nevertheless you are not allowed to use all MAC adresses, not the last the registered and registerable ones, if available from internet.

noprogrammer wrote:Just wondering if using one of these could lead to "errors" sooner or later... ?

As soon as you are using one MAC adress within one network on two different units you have problems, because they are essential for RARP, BOOTP, DHCP, ... .
Btw: If you are using the random MAC adress for a public server, and you make a companies server unavailable (as a double used MAC adress may irritate the RARP) you may have to pay compensation, if this company then loses money.
If your 'default' MAC adress causes such trouble then the network card producer is the one to pay comensation.

penpen

Btw2 i had forgotten to write this in my first post:
The only good reasons for me to use a not 'default' MAC adress is:
- if there are two units with the same MAC adress
- for backup reasons
And even then i would prefer to use a non random MAC adress.
But using random MAC adresses is oftenly mentioned by linux community, so they may have some benefit i don't know.

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Generate random mac addresses via batch?

#6 Post by noprogrammer » 11 Aug 2013 06:37

Thanks aGerman and penpen!

I've been thinkin about it a while and in order to fully make use of it I'll introduce switches.
I didn't understand the calculating stuff too much but penpen's :inToHex sub could be kept, I suppose.

I guess that Shreyas Siravara, the author of the original shell snippet, just wanted to generate random addresses with a leading 0 byte.

I pondered upon this and came up with a better idea:
1) Output a fully randomized address--like aGerman suggested - OR -
2) Output a randomized address with a leading valid manufacturer id

Now, altering penpen's script in line

Code: Select all

Set "MAC=F8-1A-67-%hex0:~-6,2%-%hex0:~-4,2%-%hex0:~-2,2%"
would do, and I thought of something like this:

Code: Select all

If [%1] Equ [] Goto :default
If [%1] Equ [-realtek] Set param=F0-DE-F1 &Goto :custom
If [%1] Equ [-tplink]  Set param=F8-1A-67 &Goto :custom
:default
Set MAC=%hex1:~-6,2%-%hex1:~-4,2%-%hex1:~-2,2%-%hex0:~-6,2%-%hex0:~-4,2%-%hex0:~-2,2%"
Goto :start
:custom
Set MAC="%param%-%hex0:~-6,2%-%hex0:~-4,2%-%hex0:~-2,2%"
:start
[...]

(Not tested yet...)

Of course there are plenty of manufacturers, though a fistful would perfectly do.
What do you think?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Generate random mac addresses via batch?

#7 Post by penpen » 11 Aug 2013 07:35

You should not fully randomize the MAC adress creation.
At least the least significant bit of the most significant byte should be 0
(unicast adress), to not irritate some protocols.

(And you should set the second least significant bit in the same byte to the value, that meets your requirements.
But then you cannot set the MAC adresses OUI to realtek or tplink)

penpen

Edited1&2 to add and remove info... finally unchanged... :?
Last edited by penpen on 11 Aug 2013 09:24, edited 2 times in total.

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Generate random mac addresses via batch?

#8 Post by noprogrammer » 11 Aug 2013 09:21

penpen wrote:You should not fully randomize the MAC adress creation.
At least the least significant bit of the most significant byte should be 0
(unicast adress), to not irritate some protocols.

I see, that's why Siravara set the first (MSB) byte to zero.

penpen wrote:(And you should set the second least significant bit in the same byte to the value, that meets your requirements.

Which is ensured by the zero byte.

penpen wrote:But then you cannot set the MAC adresses OUI to realtek or tplink)

That's correct, unless I do both: A -or- B :D

Thank you for the hint!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Generate random mac addresses via batch?

#9 Post by penpen » 11 Aug 2013 09:25

I haven't seen this on first read:
noprogrammer wrote:I didn't understand the calculating stuff too much.
set /A will handle the string to define the variable as a term over integer values with 32 bits:
"a<<b": a is shifted left by b bits
"a>>b": a is shifted right by b bits
"a&b": a and b are bitwise "anded", so
"a&0xF": is a number between 0 and 0xF (15)
%random% is a 15 bit sized random int number, so
"(%random%<<15)+%random%" is a random 30 bit int number, this is, why i haven't used the most significant byte, as its 2 most significant bits are always 0

penpen

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Generate random mac addresses via batch?

#10 Post by noprogrammer » 17 Aug 2013 08:24

Thank you penpen. I love that kind of stuff... :D

Post Reply