subnetcalc.py
class SubnetCalc(object): def __init__(self, network, mask): self.network = network.split(".") self.msk = mask.split(".") @property def subnet(self): return ".".join(self.network) @property def mask(self): return ".".join(self.msk) def _MaskToBin(self): binarymask = '' for i in self.msk: binarymask += bin(int(i))[2:].zfill(8) return binarymask @property def first(self): firstIP = self.network.copy() firstIP[3] = str(int(firstIP[3]) + 1) return ".".join(firstIP) def specifyIP(self,num): spec = self.network.copy() spec[3] = str(num) return ".".join(spec) def plusNIP(self,num): nIP = self.first.split(".") nIP[3] = str(int(nIP[3]) + int(num)) return ".".join(nIP) @property def broadcastIP(self): brcstIP = self.network.copy() fIP = self.first.split(".")[3] brcstIP[3] = int(fIP) + (int(self.countIP())) brcstIP[3] = str(brcstIP[3]) return ".".join(brcstIP) @property def last(self): lIP = self.broadcastIP.split(".") lIP[3] = str(int(lIP[3]) - 1) return ".".join(lIP) def countIP(self): mask = int('11111111111111111111111111111111',2) sub_mask = int(self._MaskToBin(),2) return int(mask ^ sub_mask) - 1
f.write(" pool{\n") f.write(" failover peer \"dhcpfailover\";\n") f.write(" range %s %s;\n" % (subnetHK.plusNIP(5),subnetHK.last)) f.write(" }") f.write("}\n") i += 1f.close()