MAC address generator in python

后端 未结 4 1472
时光说笑
时光说笑 2021-02-15 16:03

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 foll

4条回答
  •  粉色の甜心
    2021-02-15 16:36

    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

提交回复
热议问题