Skip to content

Instantly share code, notes, and snippets.

@cwilkers
Created January 8, 2022 02:08
Show Gist options
  • Save cwilkers/33d196029e5a62e8a77b222f9f7f382e to your computer and use it in GitHub Desktop.
Save cwilkers/33d196029e5a62e8a77b222f9f7f382e to your computer and use it in GitHub Desktop.
Generate VM MAC addresses from last 3 of IP range
#!/usr/bin/env python
import sys
# turn IP address string into array of int
def parseip(ipstr):
array = ipstr.split('.')
return [int(x) for x in array]
# append last 3 of IP to static MAC vendor ID and return printable string
def ip2mac(ip):
mac = [ 0x00, 0x16, 0x3e]
mac += ip[1:]
return ':'.join(map(lambda x: "%02x" % x, mac))
if __name__ == "__main__":
if (len(sys.argv) > 1):
ip = sys.argv[1]
else:
ip = "192.168.72.1"
print (ip2mac(parseip(ip)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment