def conn_seg_alloc(self, net_addr, subnet_id, host_id, l_mask, s_mask): print "正在计算并分配互联大网段空间......" #检查输入掩码 if l_mask > s_mask: print "错误:输入大网段掩码必须小于小网段掩码" return if l_mask > 32 or l_mask < 16: print "错误:输入大网段子网掩码数值超过合法范围(16-32)" return elif l_mask >= 16 and l_mask < 24: subnet_max = 2 ** (24-l_mask) host_max = 255 else: subnet_max = 1 subnet_id_start = subnet_id subnet_id_end = subnet_id_start + subnet_max host_max = seg_length if s_mask >= 16 and s_mask < 24: seg_length = 2 ** (24-s_mask) else: seg_length = 2 ** (32-s_mask) #seg_id是ip地址所对应的网段id seg_id = IPrms_Segment.where().count() sub_mask = self.cal_sub_mask(s_mask) mask = s_mask host_id_start = host_id host_id_end = 256 subnet_id_start = subnet_id subnet_id_end = subnet_id_start + subnet_max if subnet_id + subnet_max > 255: print "分配互联大网段%s.%s.%s/%s资源不足" % (net_addr, subnet_id, host_id, l_mask) return for i in range(subnet_id_start, subnet_id_end, 1): for j in range(host_id_start, host_id_end, 1): if j % seg_length == 0 and j + seg_length <= 256: seg_id = seg_id + 1 iprms_segment = IPrms_Segment() iprms_segment.segmentname = '%s.%s.%s/%s' % (net_addr, i, j, mask) iprms_segment.segmenttype = 'CONN' iprms_segment.subnetmask = sub_mask iprms_segment.gateway = 'x.x.x.x' iprms_segment.netdevicename = '' iprms_segment.ipattribute = '' iprms_segment.vlan = '' iprms_segment.min = '%s.%s.%s' % (net_addr, i, j) iprms_segment.max = '%s.%s.%s' % (net_addr, i, j+seg_length-1) iprms_segment.save() #分配互联网段时,先在iprms_ip中划分好所有ip的信息 ip = '%s.%s.%s' % (net_addr, i, j) iprms_ip = IPrms_IP() iprms_ip.segmentid = seg_id iprms_ip.ip = ip iprms_ip.state = 'Specific' iprms_ip.save() print "互联大网段分配完成"
def manage_seg_alloc(self, net_addr, subnet_id, host_id, mask, type): print "正在计算并分配管理大网段空间......" if mask > 32 or mask < 16: print "错误:输入网段子网掩码数值超过合法范围(16-32)" return elif mask >= 16 and mask < 24: subnet_num = 2 ** (24-mask) seg_length = 2 ** (24-mask) host_max = 255 else: subnet_num = 1 seg_length = 1 host_max = (2**(32-mask)) - 1 #根据掩码号计算子网掩码 sub_mask = self.cal_sub_mask(mask) seg_id = IPrms_Segment.where().count() subnet_id_start = subnet_id subnet_id_end = subnet_id + subnet_num host_id_start = 1 host_id_end = host_id_start + host_max for i in range(subnet_id_start, subnet_id_end, 1): if (i-subnet_id) % seg_length == 0: #分配管理网段时在iprms_ip、iprms_segment中划分好所有信息 seg_id = seg_id + 1 iprms_segment = IPrms_Segment() iprms_segment.segmentname = '%s.%s.%s/%s' % (net_addr, i, host_id, mask) iprms_segment.segmenttype = type iprms_segment.subnetmask = sub_mask iprms_segment.gateway = 'x.x.x.x' iprms_segment.netdevicename = '' iprms_segment.vlan = '-' iprms_segment.min = '%s.%s.0' % (net_addr, i) iprms_segment.max = '%s.%s.%s' % (net_addr, i+seg_length-1, host_max) iprms_segment.save() for j in range(host_id_start, host_id_end, 1): ip = '%s.%s.%s' % (net_addr, i, j) dev_max = j * (i+1) iprms_ip = IPrms_IP() iprms_ip.segmentid = seg_id iprms_ip.ip = ip iprms_ip.state = 'Specific' iprms_ip.save() print "管理大网段分配完成,你最多可以为%s个设备分配管理IP" % (dev_max)
def server_seg_alloc(self, type, net_addr, subnet_id, host_id, mask): print "正在计算并分配服务器大网段空间......" subnet_start = subnet_id if mask > 32 or mask < 16: print "错误:输入大网段子网掩码数值超过合法范围(16-32)" if mask >= 16 and mask < 24: subnet_num = 2 ** (24-mask) host_end = 255 else: subnet_num = 1 host_end = 2 ** (32-mask) subnet_end = subnet_start + subnet_num - 1 iprms_segment = IPrms_Segment() iprms_segment.segmentname = '%s.%s.%s/%s' % (net_addr, subnet_id, host_id, mask) iprms_segment.subnetmask = self.cal_sub_mask(mask) iprms_segment.gateway = '' iprms_segment.vlan = '' iprms_segment.segmenttype = type iprms_segment.min = '%s.%s.%s' % (net_addr, subnet_id, host_id) iprms_segment.max = '%s.%s.%s' % (net_addr, subnet_end, host_end) #jiwu提出不需要保存服务器大网段信息,如果暂时不存,那么在查询(server_query)的时候写死参数 iprms_segment.save() print "服务大网段%s.%s.%s/%s分配完成" % (net_addr, subnet_id, host_id, mask)
def regist_device(): #选择第一个未使用的ip,获取alloc的信息作为注册开始信息 tmp = IPrms_IP.where(state='Off').select_head() net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: seg_length = 256 subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) else: subnet_num = 1 seg_length = 2 ** (32-mask) x = 256 / seg_length if dev_num % x == 0: subnet_id_end = subnet_id_start + dev_num / x - 1 host_id_end = 255 else: subnet_id_end = subnet_id_start + dev_num / x host_id_end = (dev_num%x) * seg_length sub_mask = self.cal_sub_mask(mask) vlan = vlan_name dev_id1 = dev_start_id1 dev_id2 = dev_start_id2 seg_id = IPrms_Segment.where().count() #将堆叠设备号加入注册队列 register_queue = [] tuple_index = 0 for i in range(dev_start_id1, dev_end_id1 + 1, 2): pile_dev_id = (dev_id1, dev_id2) register_queue.append(pile_dev_id) dev_id1 = dev_id1 + 2 dev_id2 = dev_id2 + 2 for i in range(subnet_id_start, subnet_id_end+1, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end and j >= host_id_end: pass else: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) min_value = '%s.%s.%s' % (net_addr, i, j) max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) dev_id1 = register_queue[tuple_index][0] dev_id2 = register_queue[tuple_index][1] '''iprms_segment表注册''' iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = gateway_ip iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s/%s%s' % (dev_name, dev_id1, dev_name, dev_id2) #iprms_segment.netdeviceid = dev_id iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' for dev_id in register_queue[tuple_index]: ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) #ip_detail.netdeviceid = dev_id ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) seg_id = seg_id + 1 tuple_index = tuple_index + 1 '''iprms_ip表注册''' ip = '%s.%s.%s' % (net_addr, i, j) state = 'Available' segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: state = 'Specific' if i == subnet_id_end and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态由Off转为Unavaliable state = 'Unavailable' segmentid = 0 iprms_ip_list.append([ip, state, segmentid]) print "完成服务器网络资源自动分配"
def regist_device(): #选择最后一个被注册的ip,获取其信息推导出本次注册开始信息 tmp = IPrms_IP.where().select_tail() if tmp: net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) + 1 else: #服务网段尚未有信息被注册 se = IPrms_Segment.where(segmenttype='SE1').select_head() tmp = IPrms_Segment.where(segmenttype='CONN').select_tail() if tmp: tmp_seg = tmp.segmentname net_addr = self.get_net_addr(tmp_seg) subnet_id_start = self.get_subnet_id(tmp_seg) + 1 else: se_min_ip = se.min subnet_start = self.get_subnet_id(se_min_ip) seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: seg_length = 256 subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) else: subnet_num = 1 seg_length = 2 ** (32-mask) x = 256 / seg_length if dev_num % x == 0: subnet_id_end = subnet_id_start + dev_num / x - 1 host_id_end = 255 else: subnet_id_end = subnet_id_start + dev_num / x host_id_end = (dev_num%x) * seg_length sub_mask = self.cal_sub_mask(mask) vlan = vlan_name seg_id = IPrms_Segment.where().count() dev_id = dev_start_id for i in range(subnet_id_start, subnet_id_end+1, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end and j >= host_id_end: pass else: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) min_value = '%s.%s.%s' % (net_addr, i, j) max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) '''iprms_segment表注册''' iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = gateway_ip iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s' % (dev_name, dev_id) iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) seg_id = seg_id + 1 dev_id = dev_id + 1 '''iprms_ip表注册''' if j == 0: pass else: iprms_ip = IPrms_IP() ip = '%s.%s.%s' % (net_addr, i, j) iprms_ip.ip = ip iprms_ip.state = 'Available' iprms_ip.segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: iprms_ip.state = 'Specific' if i == subnet_id_end and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态转为Unavaliable iprms_ip.state = 'Unavailable' iprms_ip.segmentid = 0 iprms_ip_list.append(iprms_ip) print "完成服务器网络资源自动分配"
def regist_device(): #选择最后一个被注册的ip,获取其信息推导出本次注册开始信息 tmp = IPrms_IP.where().select_tail() if tmp: net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) + 1 else: #服务网段尚未有信息被注册 #tmp = IPrms_Segment.where(segmenttype='SE1').select_head() #tmp_seg = tmp.segmentname #net_addr = self.get_net_addr(tmp_seg) #subnet_id_start = self.get_subnet_id(tmp_seg) + 8 net_addr = '172.16' subnet_id_start = 8 seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: #seg_length = 256 #subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) subnet_id_end = subnet_id_start + subnet_num ##ip数量 ip_num = subnet_num * 255 else: subnet_num = 1 #seg_length = 2 ** (32-mask) #x = 256 / seg_length subnet_id_end = subnet_id_start + 1 host_id_end = dev_num # if dev_num % x == 0: # subnet_id_end = subnet_id_start + dev_num / x - 1 # host_id_end = 255 # else: # subnet_id_end = subnet_id_start + dev_num / x # host_id_end = (dev_num%x) * seg_length ##ip数量 ip_num = host_id_end + 1 if ip_num < dev_num: print "Error:网段ip资源不够分配服务器设备!" return sub_mask = self.cal_sub_mask(mask) vlan = vlan_name '''iprms_segment表注册''' min_value = '%s.%s.%s' % (net_addr, subnet_id_start, 0) max_value = '%s.%s.%s' % (net_addr, subnet_id_end - 1, host_id_end) net_seg = '%s.%s.%s/%s' % (net_addr, subnet_id_start, 0, mask) iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = '%s.%s.%s' % (net_addr, subnet_id_start, 1) iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s' % (dev_name, dev_start_id) iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) seg_id = IPrms_Segment.where().count() + 1 dev_id = dev_start_id for i in range(subnet_id_start, subnet_id_end, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 #if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end - 1 and j >= host_id_end: pass elif dev_id - dev_start_id < dev_num: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) # min_value = '%s.%s.%s' % (net_addr, i, j) # max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) '''iprms_segment表注册''' # iprms_segment = IPrms_Segment() # iprms_segment.segmentname = net_seg # iprms_segment.segmenttype = type # iprms_segment.gateway = gateway_ip # iprms_segment.subnetmask = sub_mask # iprms_segment.netdevicename = '%s%s' % (dev_name, dev_id) # #iprms_segment.netdeviceid = dev_id # iprms_segment.vlan = vlan_name # iprms_segment.ipattribute = ip_attr # iprms_segment.min = min_value # iprms_segment.max = max_value # iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) #seg_id = seg_id + 1 dev_id = dev_id + 1 '''iprms_ip表注册''' if j == 0: pass else: iprms_ip = IPrms_IP() ip = '%s.%s.%s' % (net_addr, i, j) iprms_ip.ip = ip iprms_ip.state = 'Available' if i == subnet_id_end - 1 and j >= host_id_end: iprms_ip.segmentid = 0 else: iprms_ip.segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: iprms_ip.state = 'Specific' if i == subnet_id_end - 1 and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态转为Unavaliable iprms_ip.state = 'Unavailable' iprms_ip.segmentid = 0 iprms_ip_list.append(iprms_ip) print "完成服务器网络资源自动分配"