0

Recently I copied a script from user deltaray to generate random local MAC addresses for virtual machines. I like this script because it's the easiest and most effective bash script for generating a MAC address.

However, all available private MAC addresses have four different OUI octets available for use, and my question is:

How to randomize the first set of octets for private MAC addresses and never have to worry about colliding with another VM?

x2-xx-xx-xx-xx-xx 
x6-xx-xx-xx-xx-xx
xA-xx-xx-xx-xx-xx
xE-xx-xx-xx-xx-xx

Where the "x" in x2, x6, xA and xE are randomized.

I did modify deltaray's script for a static private MAC address, but I would like to be thorough and have the best solution, and not just a single solution.

My mods:

#!/bin/bash
hexchars="0123456789ABCDEF"
mac=$( for i in {1..10} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' )
echo 02$mac

Thank you

Please see the answer to my question below.

ina2n
  • 1

2 Answers2

0

Is there a reason the VM mac addresses need to be random (pseudo random actually)? Seems like you're asking the question of how to get a guaranteed maxium period in a sequence of numbers, before overlap. A simple increment would be the most obvious, but CRC's with maximum period for a allowed min/max range exist. but I don't think you would have a problem with sequential, so CRC would be unnecessary.

With maximum period, you just have to worry about whether a VM still exists, when you wrap. If that can happen (due to the lifetime of your VMs), then you have to keep a history of running VMs, and pick another choice until you don't hit on the history. Assuming you have a wider range available, than possibly still running VMs, then you wouldn't get overlap. It's unlikely though that you need a history (if you use a sequence with maximum period, like simple increment) ..I'm guessing about the lifetime of your VMs, and number, relative to the space you have available.

Assuming there are no other mac addrs you need to worry about colliding with, other than the ones you create yourself with this algorithm. Which may not be true.

0

I figured it out.

#!/bin/bash
# This will generate every possible local MAC address available.  Works on any system that can run the bash shell.
localoctet="26AE"
hexchars="0123456789ABCDEF"
local=$( echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; echo -n ${localoctet:$(( $RANDOM % 4 )):1} )
mac=$( for i in {1..10} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' )
echo $local$mac

This will generate all possible local MAC addresses.

ina2n
  • 1