Add script to generate mac addresses

This commit is contained in:
Nico Schottelius 2020-01-12 14:35:59 +01:00
parent 53c6a14d60
commit 94dad7c9b6
2 changed files with 28 additions and 69 deletions

96
uncloud/hack/hackcloud/mac-gen.py Normal file → Executable file
View file

@ -36,7 +36,8 @@ class Mac(object):
def __init__(self): def __init__(self):
self.base_dir = "." self.base_dir = "."
self._prefix = "02:00" self.prefix = 0x002000000000
#self.prefix = "{:012x}".format(self._prefix)
self.free = self.read_file("mac-free") self.free = self.read_file("mac-free")
self.last = self.read_file("mac-last") self.last = self.read_file("mac-last")
@ -53,6 +54,11 @@ class Mac(object):
return ret return ret
def append_to_file(self, text, filename):
fname = os.path.join(self.base_dir, filename)
with open(fname, "a+") as fd:
fd.write("{}\n".format(text))
@staticmethod @staticmethod
def validate_mac(mac): def validate_mac(mac):
if not re.match(r'([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$', mac, re.I): if not re.match(r'([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$', mac, re.I):
@ -62,30 +68,24 @@ class Mac(object):
if mac in self.free: if mac in self.free:
raise Error("Mac already in free database: %s" % mac) raise Error("Mac already in free database: %s" % mac)
self._init_base_dir() self.append_to_file(mac, "mac-free")
self.free.append(mac) self.free = self.read_file("mac-free")
@staticmethod @staticmethod
def get_base_dir(): def int_to_mac(number):
return cinv.get_base_dir("db/mac") b = number.to_bytes(6, byteorder="big")
return ':'.join(format(s, '02x') for s in b)
@classmethod def getnext(self):
def exists(cls): # if self.free:
return os.path.exists(cls.get_base_dir()) # return self.free.pop()
def get_next(self): # if not self.prefix:
self._init_base_dir() # raise Error("Cannot generate address without prefix - use prefix-set")
if self.free:
return self.free.pop()
if not self.prefix:
raise Error("Cannot generate address without prefix - use prefix-set")
if self.last: if self.last:
suffix = re.search(r'([0-9A-F]{2}[-:]){2}[0-9A-F]{2}$', self.last, re.I) last_number = int(self.last[0], 16)
last_number_hex = "0x%s" % suffix.group().replace(":", "")
last_number = int(last_number_hex, 16)
if last_number == int('0xffffff', 16): if last_number == int('0xffffff', 16):
raise Error("Exhausted all possible mac addresses - try to free some") raise Error("Exhausted all possible mac addresses - try to free some")
@ -94,60 +94,16 @@ class Mac(object):
else: else:
next_number = 0 next_number = 0
next_number_hex = "%0.6x" % next_number next_number_string = "{:012x}".format(next_number)
next_suffix = "%s:%s:%s" % (next_number_hex[0:2], next_number_hex[2:4], next_number_hex[4:6])
next_mac = "%s:%s" % (self.prefix, next_suffix) next_mac_number = self.prefix + next_number
next_mac = self.int_to_mac(next_mac_number)
self.last = next_mac with open(os.path.join(self.base_dir, "mac-last"), "w+") as fd:
fd.write("{}\n".format(next_number_string))
return next_mac return next_mac
@property
def prefix(self):
return self._prefix
@prefix.setter
def prefix(self, prefix):
if not re.match(r'([0-9A-F]{2}[-:]){2}[0-9A-F]{2}$', prefix, re.I):
raise Error("Wrong mac address format - use 00:11:22")
self._init_base_dir()
self._prefix = prefix
@classmethod
def commandline_generate(cls, args):
mac = Mac()
print(mac.get_next())
@classmethod
def commandline_free_add(cls, args):
mac = Mac()
mac.validate_mac(args.address)
mac.free_append(args.address)
@classmethod
def commandline_free_list(cls, args):
mac = Mac()
for mac in mac.free:
print(mac)
@classmethod
def commandline_prefix_set(cls, args):
mac = Mac()
mac.prefix = args.prefix
@classmethod
def commandline_prefix_get(cls, args):
mac = cls()
print(mac.prefix)
@classmethod
def commandline_add(cls, args):
host = cls(fqdn=args.fqdn)
host.host_type = args.type
@classmethod @classmethod
def commandline(cls): def commandline(cls):
pass pass
@ -156,4 +112,6 @@ class Mac(object):
if __name__ == '__main__': if __name__ == '__main__':
m = Mac() m = Mac()
m.commandline() m.commandline()
print(m.free) # print(m.free)
#print(m.last)
print(m.getnext())

View file

@ -0,0 +1 @@
000000000006