MAC address generator in python
Asked Answered
T

6

12

For purpose of creating bulk virtual machines, I need to create a random MAC address generator in Python. I don't know how to generate random MAC addresses.

Is the following program correct or not?

import random

# The first line is defined for specified vendor
mac = [ 0x00, 0x24, 0x81,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]

print ':'.join(map(lambda x: "%02x" % x, mac))
Tutto answered 13/12, 2011 at 5:47 Comment(6)
Are you trying to generate MAC addresses specifically?Sudderth
yes manufacture specificTutto
That sample program is fine for a single MAC address but it runs the very real risk that you'll generate identical fake MAC addresses for multiple machines, which might lead to odd networking problems.Sudderth
how to solve the above problemTutto
Incidentally, you should pick a number that is not yet allocated.Sudderth
@DEEPAKGEORGE, are you using VMWare, if so... be specific about the VMWare product you are using. VMWare cares how you build the specific MacAddressShuntwound
X
16

For anyone wanting to generate their own MAC addresses (a good example is for VM NICs), you probably just want this:

"02:00:00:%02x:%02x:%02x" % (random.randint(0, 255),
                             random.randint(0, 255),
                             random.randint(0, 255))

Or, if you want to do this in a unix'y shell, this works on many:

printf '02:00:00:%02X:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))

This gives you a unicast MAC address that is 100% safe to use in your environment, and isn't trampling on anyone's registered MAC address space.


More detail...

The bottom two bits of the top byte (0x02) give you a locally administered unicast address, which is probably what you want if you are hitting stackoverflow for how to generate this. :)

If the MAC address is not locally administered, it means it is supposed to be "globally unique". MAC addresses in this category are centrally registered with the IEEE, and you should have a unique OUI (Organizationally Unique Identifier) issued to you by the IEEE. See this link for the global registry of OUI values. This OUI value ends up in the first 3 bytes (or just the top 22 bits, really).

MAC addresses aren't that complicated, so you should probably also just have a look at the definition. Wikipedia has a good one.

Xerography answered 21/4, 2017 at 15:20 Comment(1)
"52:54:00:%02x:%02x:%02x" % tuple(random.randint(0, 255) for v in range(3)) qemu specific example.Roderich
L
5

Modified from mamofish's code to Python3:

mac = '00:00:00:'
for number in range(16**6):
    hex_num = hex(number)[2:].zfill(6)
    print("{}{}{}:{}{}:{}{}".format(mac,*hex_num))

Generates mac addresses as strings from 00:00:00:00:00:00 to 00:00:00:ff:ff:ff.

Lumbard answered 20/5, 2016 at 19:15 Comment(2)
The link in this answer is now a loan scam site. Just FYI.Incisor
Removed broken linkLumbard
P
1

Since uniqueness is all you should care about (beyond making the address well-formed), I'd worry about the MSBs in the OUI and use a sequence in the NIC specific bytes. The distribution of the addresses is likely unimportant to your application (even though some NIC or switch implementations might use them as an input to a hash, this is likely not to be a big concern).

You may want to consider using the "locally administered" flag in the OUI to avoid a conflict with an existing device manufacturer.

Avoid pitfalls like setting the multicast bit (your example does).

Potency answered 13/12, 2011 at 6:19 Comment(0)
G
0

To avoid duplicates:

If you're going to generate a LOT (millions) of such MAC addresses, you might want to generate an in-order list of MAC's, feed that to a linear randomization process (GNU sort -R should do fine - I don't think it does this in linear time, but it has a similar end result) once, and then pull your fresh addresses off one end of the randomized list as needed. I believe such a list should fit in about 34 megabytes.

If you merely need thousands, you're probably better off maintaining a text file with already-selected values, and checking for collisions against that, adding new values as you go. This is a slower algorithm asympotically speaking, but it has a much less overhead, so it should still be a win for lower numbers of mac addresses.

BTW, should the 4th octet (numbered from 1 starting on the left), be 0-ff instead of 0-7f? I see no occurrences of 7f or 127 in the Wikipedia page on Mac addresses: http://en.wikipedia.org/wiki/MAC_address

Gaal answered 13/12, 2011 at 7:35 Comment(0)
C
0

For those who are here for simple one-liner, with no care of being repetitive:

import random
print(":".join([('0'+hex(random.randint(0,256))[2:])[-2:].upper() for _ in range(6)]))

## '8A:16:71:27:0D:1D'
Cleo answered 9/8, 2023 at 8:47 Comment(0)
U
0

With python the easiest way is to use generate_mac package. Once you've installed it with pip (or whatever you use):

from generate_mac import generate_mac
random_mac_address = generate_mac.total_random()

This generates a random mac address e.g.: AC:A6:10:2C:F9:7E

Underclothes answered 8/2 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.