def network_get_subnet(self, network): """ Function return virtual network info: ip, netmask, dhcp, type forward. """ net = self.conn.networkLookupByName(network) xml_net = net.XMLDesc(0) ipv4 = [] fw = util.get_xml_path(xml_net, "/network/forward/@mode") forwardDev = util.get_xml_path(xml_net, "/network/forward/@dev") if fw: ipv4.append([fw, forwardDev]) else: ipv4.append(None) # Subnet block addrStr = util.get_xml_path(xml_net, "/network/ip/@address") netmaskStr = util.get_xml_path(xml_net, "/network/ip/@netmask") if addrStr and netmaskStr: netmask = IP(netmaskStr) gateway = IP(addrStr) network = IP(gateway.int() & netmask.int()) ipv4.append(IP(str(network) + "/" + netmaskStr)) else: ipv4.append(None) # DHCP block dhcpstart = util.get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@start") dhcpend = util.get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@end") if not dhcpstart or not dhcpend: pass else: ipv4.append([IP(dhcpstart), IP(dhcpend)]) return ipv4
def network_get_subnet(self, network): """ Function return virtual network info: ip, netmask, dhcp, type forward. """ net = self.networkPool(network) xml_net = net.XMLDesc(0) ipv4 = [] fw = get_xml_path(xml_net, "/network/forward/@mode") forwardDev = get_xml_path(xml_net, "/network/forward/@dev") if fw: ipv4.append([fw, forwardDev]) else: ipv4.append(None) # Subnet block addrStr = get_xml_path(xml_net, "/network/ip/@address") netmaskStr = get_xml_path(xml_net, "/network/ip/@netmask") if addrStr and netmaskStr: netmask = IP(netmaskStr) gateway = IP(addrStr) network = IP(gateway.int() & netmask.int()) ipv4.append(IP(str(network) + "/" + netmaskStr)) else: ipv4.append(None) # DHCP block dhcpstart = get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@start") dhcpend = get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@end") if not dhcpstart or not dhcpend: pass else: ipv4.append([IP(dhcpstart), IP(dhcpend)]) return ipv4
def search_ip(self, geoloc, scriptsJS, ip_range): main_thread = threading.currentThread() print "########### Search by IP ###########" ips = [] domaines = self.db.new_domaines.find() thread_pool = [] cache = {} for domaine in domaines: try: ips.append(domaine['ip']) except KeyError: print domaine i = 0 print 'les IPS sont: ' + str(ips) ip_to_add = [] if ip_range: ip_to_add = [str(x) for x in IP(ip_range)] ips[len(ips):] = ip_to_add for ip in set(ips): if ip != '0.0.0.0': i += 1 gs = search.search(20, 'ip:' + str(ip), scriptsJS[1], self.db_value) gs.start() thread_pool.append(gs) if i % 10 == 0: for t in thread_pool: t.join() for t in thread_pool: t.record() print "########### Search terminated ###########" print "########### Search by network ###########" print "########### Resolve IP ############" networks.resolve(geoloc, self.db_value)
def network(request, host_id, pool): """ Networks block """ if not request.user.is_authenticated(): return HttpResponseRedirect("/login") errors = [] host = Host.objects.get(id=host_id) try: conn = ConnServer(host) except libvirtError as e: conn = None if not conn: errors.append(e.message) else: networks = conn.networks_get_node() if pool is None: if len(networks) == 0: return HttpResponseRedirect("/network/%s/add/" % (host_id)) else: return HttpResponseRedirect("/network/%s/%s/" % (host_id, networks.keys()[0])) if pool == "add": if request.method == "POST": if "addpool" in request.POST: dhcp = [] pool_name = request.POST.get("name", "") net_addr = request.POST.get("net_addr", "") forward = request.POST.get("forward", "") dhcp.append(request.POST.get("dhcp", "")) name_have_symbol = re.search("[^a-zA-Z0-9\_\-]+", pool_name) ip_have_symbol = re.search("[^0-9\.\/]+", net_addr) if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The pool name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _("The pool name must not contain any special characters") errors.append(msg) if not net_addr: msg = _("No subnet has been entered") errors.append(msg) elif ip_have_symbol: msg = _("The pool name must not contain any special characters") errors.append(msg) if pool_name in networks.keys(): msg = _("Pool name already in use") errors.append(msg) try: netmask = IP(net_addr).strNetmask() ipaddr = IP(net_addr) gateway = ipaddr[0].strNormal()[-1] if gateway == "0": gw = ipaddr[1].strNormal() dhcp_start = ipaddr[2].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() else: gw = ipaddr[0].strNormal() dhcp_start = ipaddr[1].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() dhcp.append(dhcp_start) dhcp.append(dhcp_end) except: msg = _("Input subnet pool error") errors.append(msg) if not errors: try: conn.new_network_pool(pool_name, forward, gw, netmask, dhcp) return HttpResponseRedirect("/network/%s/%s/" % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = conn.vds_get_node() info = conn.network_get_info(pool) if info[0] == True: ipv4_net = conn.network_get_subnet(pool) if request.method == "POST": net = conn.networkPool(pool) if "start" in request.POST: try: net.create() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if "stop" in request.POST: try: net.destroy() return HttpResponseRedirect(request.get_full_path()) except libvirtError as error_msg: errors.append(error_msg.message) if "delete" in request.POST: try: net.undefine() return HttpResponseRedirect("/network/%s/" % host_id) except libvirtError as error_msg: errors.append(error_msg.message) conn.close() return render_to_response("network.html", locals(), context_instance=RequestContext(request))
def network(request, host_id, pool): """ Networks block """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') host = Host.objects.get(id=host_id) conn = libvirt_func.libvirt_conn(host) if type(conn) == dict: return HttpResponseRedirect('/overview/%s/' % host_id) else: networks = libvirt_func.networks_get_node(conn) if pool is None: if len(networks) == 0: return HttpResponseRedirect('/network/%s/add/' % (host_id)) else: return HttpResponseRedirect('/network/%s/%s/' % (host_id, networks.keys()[0])) if pool == 'add': if request.method == 'POST': if 'addpool' in request.POST: dhcp = [] pool_name = request.POST.get('name', '') net_addr = request.POST.get('net_addr', '') forward = request.POST.get('forward', '') dhcp.append(request.POST.get('dhcp', '')) name_have_symbol = re.search('[^a-zA-Z0-9\_\-]+', pool_name) ip_have_symbol = re.search('[^0-9\.\/]+', net_addr) errors = [] if not pool_name: msg = _("No pool name has been entered") errors.append(msg) elif len(pool_name) > 12: msg = _("The pool name must not exceed 20 characters") errors.append(msg) else: if name_have_symbol: msg = _( "The pool name must not contain any special characters" ) errors.append(msg) if not net_addr: msg = _("No subnet has been entered") errors.append(msg) elif ip_have_symbol: msg = _( "The pool name must not contain any special characters" ) errors.append(msg) if pool_name in networks.keys(): msg = _("Pool name already in use") errors.append(msg) try: netmask = IP(net_addr).strNetmask() ipaddr = IP(net_addr) gateway = ipaddr[0].strNormal()[-1] if gateway == '0': gw = ipaddr[1].strNormal() dhcp_start = ipaddr[2].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() else: gw = ipaddr[0].strNormal() dhcp_start = ipaddr[1].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() dhcp.append(dhcp_start) dhcp.append(dhcp_end) except: msg = _("Input subnet pool error") errors.append(msg) if not errors: try: libvirt_func.new_network_pool( conn, pool_name, forward, gw, netmask, dhcp) net = conn.networkLookupByName(pool_name) net.create() net.setAutostart(1) return HttpResponseRedirect('/network/%s/%s/' % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = libvirt_func.vds_get_node(conn) net = conn.networkLookupByName(pool) info = libvirt_func.network_get_info(net) if info[0] == True: ipv4_net = libvirt_func.network_get_subnet(net) if request.method == 'POST': if 'start' in request.POST: try: net.create() return HttpResponseRedirect('/network/%s/%s' % (host_id, pool)) except libvirtError as error_msg: errors.append(error_msg.message) if 'stop' in request.POST: try: net.destroy() except libvirtError as error_msg: errors.append(error_msg.message) return HttpResponseRedirect('/network/%s/%s' % (host_id, pool)) if 'delete' in request.POST: try: net.undefine() except libvirtError as error_msg: errors.append(error_msg.message) return HttpResponseRedirect('/network/%s/' % host_id) conn.close() return render_to_response('network.html', locals(), context_instance=RequestContext(request))