Skip to content

Instantly share code, notes, and snippets.

@pklaus
Last active May 31, 2025 14:15
Show Gist options
  • Select an option

  • Save pklaus/9638536 to your computer and use it in GitHub Desktop.

Select an option

Save pklaus/9638536 to your computer and use it in GitHub Desktop.

Revisions

  1. pklaus revised this gist Sep 4, 2019. 2 changed files with 38 additions and 15 deletions.
    15 changes: 8 additions & 7 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    The mini-tool has a CLI-Interface with the following options:

    * Unicast or Multicast? Default: Unicast
    * Locally Administered or Globally Unique? Default: Locally Administered
    * Prescribe specific OUI (overwrites the above two)

    TODO

    * Create a CLI-Interface with the following options:
    * Unicast or Multicast? Default: Unicast
    * Locally Administered or Globally Unique? Default: Locally Administered
    * Prescribe specific OUI (overwrites the above two)
    * Number of MACs to generate (they should not collide and be piped out separated by newlines)
    * Add an option to generate a number of MACs without collisions.

    ### Resources

    * [MAC Adress](http://en.wikipedia.org/wiki/MAC_address) on Wikipedia
    * redhat's virtualization guide on [Generating a new unique MAC address](http://goo.gl/rtt5T8)
    * A Perl Script that does the same: [randmac.pl](http://www.hellion.org.uk/cgi-bin/randmac.pl?source=1)
    * The python module [`netaddr`](https://pypi.python.org/pypi/netaddr) can help you work with MAC addresses.

    * The python module [`netaddr`](https://pypi.python.org/pypi/netaddr) can help you work with MAC addresses.
    38 changes: 30 additions & 8 deletions randmac.py
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,36 @@

    import random

    def randomMAC():
    return [ 0x00, 0x16, 0x3e,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]
    def random_bytes(num=6):
    return [random.randrange(256) for _ in range(num)]

    def MACprettyprint(mac):
    return ':'.join(map(lambda x: "%02x" % x, mac))
    def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='%02x'):
    mac = random_bytes()
    if oui:
    if type(oui) == str:
    oui = [int(chunk) for chunk in oui.split(separator)]
    mac = oui + random_bytes(num=6-len(oui))
    else:
    if multicast:
    mac[0] |= 1 # set bit 0
    else:
    mac[0] &= ~1 # clear bit 0
    if uaa:
    mac[0] &= ~(1 << 1) # clear bit 1
    else:
    mac[0] |= 1 << 1 # set bit 1
    return separator.join(byte_fmt % b for b in mac)

    def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--uaa', action='store_true', help='generates a universally administered address (instead of LAA otherwise)')
    parser.add_argument('--multicast', action='store_true', help='generates a multicast MAC (instead of unicast otherwise)')
    parser.add_argument('--oui', help='enforces a specific organizationally unique identifier (like 00:60:2f for Cisco)')
    parser.add_argument('--byte-fmt', default='%02x', help='The byte format. Set to %02X for uppercase hex formatting.')
    parser.add_argument('--separator', default=':', help="The byte separator character. Defaults to ':'.")
    args = parser.parse_args()
    print(generate_mac(oui=args.oui, uaa=args.uaa, multicast=args.multicast, separator=args.separator, byte_fmt=args.byte_fmt))

    if __name__ == '__main__':
    print(MACprettyprint(randomMAC()))
    main()
  2. pklaus revised this gist Mar 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion randmac.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    import random

    def randomMAC():
    mac = [ 0x00, 0x16, 0x3e,
    return [ 0x00, 0x16, 0x3e,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]
  3. pklaus revised this gist Mar 19, 2014. 2 changed files with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,6 @@ TODO

    * [MAC Adress](http://en.wikipedia.org/wiki/MAC_address) on Wikipedia
    * redhat's virtualization guide on [Generating a new unique MAC address](http://goo.gl/rtt5T8)
    * A Perl Script that does the same: [randmac.pl](http://www.hellion.org.uk/cgi-bin/randmac.pl?source=1)
    * A Perl Script that does the same: [randmac.pl](http://www.hellion.org.uk/cgi-bin/randmac.pl?source=1)
    * The python module [`netaddr`](https://pypi.python.org/pypi/netaddr) can help you work with MAC addresses.

    Empty file modified randmac.py
    100644 → 100755
    Empty file.
  4. pklaus created this gist Mar 19, 2014.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    TODO

    * Create a CLI-Interface with the following options:
    * Unicast or Multicast? Default: Unicast
    * Locally Administered or Globally Unique? Default: Locally Administered
    * Prescribe specific OUI (overwrites the above two)
    * Number of MACs to generate (they should not collide and be piped out separated by newlines)

    ### Resources

    * [MAC Adress](http://en.wikipedia.org/wiki/MAC_address) on Wikipedia
    * redhat's virtualization guide on [Generating a new unique MAC address](http://goo.gl/rtt5T8)
    * A Perl Script that does the same: [randmac.pl](http://www.hellion.org.uk/cgi-bin/randmac.pl?source=1)
    15 changes: 15 additions & 0 deletions randmac.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #!/usr/bin/env python

    import random

    def randomMAC():
    mac = [ 0x00, 0x16, 0x3e,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]

    def MACprettyprint(mac):
    return ':'.join(map(lambda x: "%02x" % x, mac))

    if __name__ == '__main__':
    print(MACprettyprint(randomMAC()))