Пример #1
0
def sif(ipfset, site):
    if not site:
        return [], None
    misc = set()
    networks = site.network_set.all()
    if not networks:
        return None
    parent_net = calc_parent(networks[0])
    if not parent_net:
        parent_net = networks[0]
    misc.add(site)
    misc.add(parent_net)
    parent_net.update_ipf()
    ipfset.add(parent_net.ipf)
    return ipfset, misc
Пример #2
0
def sif(ipfset, site):
    if not site:
        return [], None
    misc = set()
    networks = site.network_set.all()
    if not networks:
        return None
    parent_net = calc_parent(networks[0])
    if not parent_net:
        parent_net = networks[0]
    misc.add(site)
    misc.add(parent_net)
    parent_net.update_ipf()
    ipfset.add(parent_net.ipf)
    return ipfset, misc
Пример #3
0
def sif(site):
    if not site:
        return [], None
    misc = set()
    networks = site.network_set.all()
    if not networks:
        return None
    parent_net = calc_parent(networks[0])
    if not parent_net:
        parent_net = networks[0]
    misc.add(site)
    misc.add(parent_net)
    # Return list to make consistent with vif
    parent_net.update_network()
    return [ipf(int(parent_net.network.network),
                int(parent_net.network.broadcast), root=False)], misc
Пример #4
0
def sif(site):
    if not site:
        return [], None
    misc = set()
    networks = site.network_set.all()
    if not networks:
        return None
    parent_net = calc_parent(networks[0])
    if not parent_net:
        parent_net = networks[0]
    misc.add(site)
    misc.add(parent_net)
    # Return list to make consistent with vif
    parent_net.update_network()
    return [
        ipf(int(parent_net.network.network),
            int(parent_net.network.broadcast),
            root=False)
    ], misc
Пример #5
0
def create_network_vlan(v_num, v_name, n_str, site_octs, router_label=None):
    print "##### {0} {1} ##### {2} #####".format(v_num, v_name, n_str, site_octs)

    v, _ = Vlan.objects.get_or_create(number=v_num, name=v_name)

    for so in site_octs:
        n, _ = Network.objects.get_or_create(network_str=n_str.format(so), ip_type="4")
        n.vlan = v
        parent = calc_parent(n)
        if parent:
            n.site = parent.site
        n.save()
        if str(so) == str(20):
            n.site = Site.objects.get(name="phx1")
            n.save()
        ip = ipaddr.IPv4Network(n_str.format(so))
        if int(n_str[-2:]) >= 22:
            r, _ = Range.objects.get_or_create(
                start_str=str(ipaddr.IPv4Address(int(ip.network) + 10)),
                end_str=str(ipaddr.IPv4Address(int(ip.broadcast) - 2)),
                network=n,
            )
Пример #6
0
def create_network_vlan(v_num, v_name, n_str, site_octs, router_label=None):
    print "##### {0} {1} ##### {2} #####".format(v_num, v_name, n_str,
                                                 site_octs)

    v, _ = Vlan.objects.get_or_create(number=v_num, name=v_name)

    for so in site_octs:
        n, _ = Network.objects.get_or_create(network_str=n_str.format(so),
                                             ip_type='4')
        n.vlan = v
        parent = calc_parent(n)
        if parent:
            n.site = parent.site
        n.save()
        if str(so) == str(20):
            n.site = Site.objects.get(name="phx1")
            n.save()
        ip = ipaddr.IPv4Network(n_str.format(so))
        if int(n_str[-2:]) >= 22:
            r, _ = Range.objects.get_or_create(
                start_str=str(ipaddr.IPv4Address(int(ip.network) + 10)),
                end_str=str(ipaddr.IPv4Address(int(ip.broadcast) - 2)),
                network=n)
Пример #7
0
def migrate_networks():
    all = truth.models.Truth.objects.all()
    print all
    scopes = []
    for thing in all:
        if thing.name.find("-vlan") != -1:
            scopes.append(thing)
    vlan_number_re = re.compile("(\d+)$")
    is_network_start = re.compile("dhcp.scope.start")
    is_network_end = re.compile("dhcp.scope.end")
    is_netmask = re.compile("dhcp.scope.netmask")
    is_pool_start = re.compile("dhcp.pool.start")
    is_pool_end = re.compile("dhcp.pool.end")
    is_ntp_server = re.compile("dhcp.option.ntp_server.\d")
    is_domain_name = re.compile("dhcp.option.domain_name.\d")
    is_dns_servers = re.compile("dhcp.option.dns_server.\d")
    is_pool_allow_booting = re.compile("dhcp.pool.allow_booting.\d")
    is_pool_allow_bootp = re.compile("dhcp.pool.allow_bootp.\d")
    for scope in scopes:
        if str(scope).endswith("fake"):
            print "Skipping " + str(scope)
            continue
        vlan_number = vlan_number_re.search(str(scope)).groups(1)[0]
        network_start = None
        network_end = None
        netmask = None
        pool_start = None
        pool_end = None
        ntp_servers = []
        domain_name = None
        dns_servers = []
        allow_booting = None
        allow_bootp = None
        for kv in scope.keyvalue_set.all():
            if is_network_start.match(kv.key):
                network_start = kv.value
            if is_network_end.match(kv.key):
                network_end = kv.value
            if is_netmask.match(kv.key):
                netmask = kv.value
            if is_pool_start.match(kv.key):
                pool_start = kv.value
            if is_pool_end.match(kv.key):
                pool_end = kv.value
            if is_ntp_server.match(kv.key):
                ntp_servers.append(kv.value)
            if is_domain_name.match(kv.key):
                domain_name = kv.value
            if is_dns_servers.match(kv.key):
                dns_servers.append(kv.value)
            if is_pool_allow_booting.match(kv.key):
                allow_booting = bool(kv.value)
            if is_pool_allow_bootp.match(kv.key):
                allow_bootp = bool(kv.value)

        vlan = Vlan.objects.filter(number=vlan_number)
        try:
            net = ipaddr.IPv4Network(network_start + '/' + netmask)
        except ipaddr.AddressValueError, e:
            if str(scope) == "phx1-vlan75":
                net = ipaddr.IPv4Network("10.8.75.0/24")
            else:
                pdb.set_trace()
                continue
        network = Network.objects.filter(ip_lower=int(net.network),
                                         ip_upper=0, prefixlen=net.prefixlen)

        print "=" * 20 + " " + str(scope)
        print "Expected Vlan: " + str(vlan)
        print vlan_number
        if not network:
            print "Creating new Network: " + str(net)
            network, _ = Network.objects.get_or_create(
                network_str=str(net), ip_type='4')
            parent = calc_parent(network)
            if parent:
                network.site = parent.site
        else:
            network = network[0]
            print "Existing Network: " + str(network)

        if vlan:
            network.vlan = vlan[0]
        else:
            v, _ = Vlan.objects.get_or_create(
                name="I need a name.", number=vlan_number)
            network.vlan = v
        network.save()

        print network_start
        print network_end
        print netmask
        print pool_start
        print pool_end
        print ntp_servers
        print domain_name
        print dns_servers
        print allow_booting
        print allow_bootp
        if str(scope) == "phx1-vlan75":
            continue
        if ntp_servers:
            real_ntp_servers = []
            for server in ntp_servers:
                if server == '':
                    continue
                else:
                    real_ntp_servers.append(server)
            if real_ntp_servers:
                kv = NetworkKeyValue(
                    key="ntp-servers", value=", ".join(real_ntp_servers),
                        network=network)
                try:
                    kv.clean()
                    kv.save()
                except IntegrityError, e:
                    # Duplicate error
                    pass
Пример #8
0
def search_ip(request):
    if request.method == "POST":
        form = IpSearchForm(request.POST)
        try:
            if form.is_valid():
                ip_type = form.cleaned_data['ip_type']
                search_ip = form.cleaned_data['search_ip']
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(search_ip)
                    if ip_type == '6':
                        network = ipaddr.IPv6Network(search_ip)
                except ipaddr.AddressValueError, e:
                    form._errors['__all__'] = ErrorList(
                        ["Bad IPv{0} Address {1}".format(ip_type, search_ip)])
                    return render(request, 'cydhcp/cydhcp_form.html',
                                  {'form': form})
                try:
                    network = Network.objects.get(network_str=search_ip)
                    search_ip = network
                    found_exact = True
                except ObjectDoesNotExist:
                    found_exact = False
                    network = Network(ip_type,
                                      network_str=search_ip,
                                      ip_type=ip_type)
                parent = calc_parent(network)
                eldars, sub_networks = calc_networks(network)
                if ip_type == '6':
                    sip_upper, sip_lower = ipv6_to_longs(network.network.ip)
                    eip_upper, eip_lower = ipv6_to_longs(
                        network.network.broadcast)
                else:
                    sip_upper, sip_lower = 0, int(network.network.ip)
                    eip_upper, eip_lower = 0, int(network.network.broadcast)

                addrs_count = AddressRecord.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if addrs_count > 100:
                    addrs = None  # This is too much
                else:
                    addrs = AddressRecord.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

                ptrs_count = PTR.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if ptrs_count > 100:
                    ptrs = None  # This is too much
                else:
                    ptrs = PTR.objects.filter(ip_upper__gte=sip_upper,
                                              ip_lower__gte=sip_lower,
                                              ip_upper__lte=eip_upper,
                                              ip_lower__lte=eip_lower)

            return render(
                request, 'cydhcp/cydhcp_results.html', {
                    'search_ip': search_ip,
                    'found_exact': found_exact,
                    'addrs': addrs,
                    'addrs_count': addrs_count,
                    'ptrs_count': ptrs_count,
                    'ptrs': ptrs,
                    'parent': parent,
                    'eldars': eldars,
                    'sub_networks': sub_networks,
                })
        except ValidationError, e:
            form._errors['__all__'] = ErrorList(e.messages)
            return render(request, 'cydhcp/cydhcp_form.html', {'form': form})
Пример #9
0
def search_ip(request):
    if request.method == "POST":
        form = IpSearchForm(request.POST)
        try:
            if form.is_valid():
                ip_type = form.cleaned_data['ip_type']
                search_ip = form.cleaned_data['search_ip']
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(search_ip)
                    if ip_type == '6':
                        network = ipaddr.IPv6Network(search_ip)
                except ipaddr.AddressValueError, e:
                    form._errors['__all__'] = ErrorList(
                        ["Bad IPv{0} Address {1}".format(ip_type, search_ip)])
                    return render(request, 'cydhcp/cydhcp_form.html', {
                        'form': form
                    })
                try:
                    network = Network.objects.get(network_str=search_ip)
                    search_ip = network
                    found_exact = True
                except ObjectDoesNotExist:
                    found_exact = False
                    network = Network(ip_type, network_str=search_ip,
                                      ip_type=ip_type)
                parent = calc_parent(network)
                eldars, sub_networks = calc_networks(network)
                if ip_type == '6':
                    sip_upper, sip_lower = ipv6_to_longs(network.network.ip)
                    eip_upper, eip_lower = ipv6_to_longs(
                        network.network.broadcast)
                else:
                    sip_upper, sip_lower = 0, int(network.network.ip)
                    eip_upper, eip_lower = 0, int(network.network.broadcast)

                addrs_count = AddressRecord.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if addrs_count > 100:
                    addrs = None  # This is too much
                else:
                    addrs = AddressRecord.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

                ptrs_count = PTR.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if ptrs_count > 100:
                    ptrs = None  # This is too much
                else:
                    ptrs = PTR.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

            return render(request, 'cydhcp/cydhcp_results.html', {
                'search_ip': search_ip,
                'found_exact': found_exact,
                'addrs': addrs,
                'addrs_count': addrs_count,
                'ptrs_count': ptrs_count,
                'ptrs': ptrs,
                'parent': parent,
                'eldars': eldars,
                'sub_networks': sub_networks,
            })
        except ValidationError, e:
            form._errors['__all__'] = ErrorList(e.messages)
            return render(request, 'cydhcp/cydhcp_form.html', {
                'form': form
            })