Tuesday, October 06, 2015

Oracle Linux - generate MAC address

In most cases you will not need to generate a MAC address. It will come with your network interface or, in cases of a virtual machine, it will be generated for you by the orchestration tooling. However, in some cases you might need to generate a random MAC address. In my case this was when we experimented with the Oracle VM API's and at some point in time we wanted to provide the MAC address to the code that orchestrated the creation and deployment of a new VM.

Generating a new MAC address can be done in multiple ways, the below Python script is just one of the examples, however, it can be intergrated faitly easy into wider Python code or you can call it from a Bash script.

#!/usr/bin/python
# macgen.py script to generate a MAC address for guests on Xen
#
import random
#
def randomMAC():
 mac = [ 0x00, 0x16, 0x3e,
  random.randint(0x00, 0x7f),
  random.randint(0x00, 0xff),
  random.randint(0x00, 0xff) ]
 return ':'.join(map(lambda x: "%02x" % x, mac))
#
print randomMAC()

This script will simply provide you a complete random MAC address.
Post a Comment