Skip to content

Instantly share code, notes, and snippets.

@EONRaider
Last active October 23, 2020 18:50
Show Gist options
  • Save EONRaider/ebda160f27ce0a09f1ab1c7733ba0e8c to your computer and use it in GitHub Desktop.
Save EONRaider/ebda160f27ce0a09f1ab1c7733ba0e8c to your computer and use it in GitHub Desktop.
Generate a random MAC address in Python 3
#!/usr/bin/env python3
# https://gist.github.com/EONRaider/ebda160f27ce0a09f1ab1c7733ba0e8c
__author__ = 'EONRaider, keybase.io/eonraider'
import random
def randomize_mac(*, sep: str = ':') -> str:
"""
Generates random MAC addresses in the format XX:XX:XX:XX:XX:XX by
default, where X is a hexadecimal value.
Args:
sep (str): The separator to be applied between each octet.
IEEE 802 defines the acceptable characters as ':' and '-'.
"""
hex_values = '0123456789ABCDEF'
octets = (''.join(random.choices(hex_values, k=2)) for _ in range(6))
return sep.join(octets)
if __name__ == '__main__':
for _ in range(3):
print(randomize_mac())
print(randomize_mac(sep='-'))
# Sample output:
# A8:5A:15:08:85:91
# 75-F1-EC-B4-BB-64
# 19:A5:DC:A1:E1:C2
# D9-5A-F1-33-B7-B0
# 7E:FD:95:FF:9E:B5
# AE-EA-F8-E5-39-4E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment