Last active
May 31, 2025 14:15
-
-
Save pklaus/9638536 to your computer and use it in GitHub Desktop.
Revisions
-
pklaus revised this gist
Sep 4, 2019 . 2 changed files with 38 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 * 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,36 @@ import random def random_bytes(num=6): return [random.randrange(256) for _ in range(num)] 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__': main() -
pklaus revised this gist
Mar 19, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import random def randomMAC(): return [ 0x00, 0x16, 0x3e, random.randint(0x00, 0x7f), random.randint(0x00, 0xff), random.randint(0x00, 0xff) ] -
pklaus revised this gist
Mar 19, 2014 . 2 changed files with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) * The python module [`netaddr`](https://pypi.python.org/pypi/netaddr) can help you work with MAC addresses. Empty file. -
pklaus created this gist
Mar 19, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()))