예제 #1
0
파일: views.py 프로젝트: rollerd/django-dns
def text_reverse(request, name):
    """
    Displays data for reverse text view. Index redirects here.
    Generates IP list from model, reverses each IP in the list and appends the .in-addr.arpa suffix
    Returns direct response in plaintext format

    arguments:
        request: request object
        name: subnet name
    """
    response = HttpResponse(content_type="text/plain")
    if not Mapping.objects.filter(subnet_id=name):
        response.write("ERROR: NO SUBNET WITH NAME: %s FOUND textreverse" % name)
    else:
        mapping = Mapping.objects.filter(subnet_id=name)
        unsorted_ip_list = []
        for obj in mapping:
            if pylib.is_valid_ip(obj.ip_or_cname):
                unsorted_ip_list.append(obj.ip_or_cname)
        sorted_ip_list = sorted(unsorted_ip_list, key=lambda octet: socket.inet_aton(octet))
        header = pylib.get_header("@")
        response.write(header)
        for ip in sorted_ip_list:
            reversed_ip = pylib.reverse_ip(ip)
            # obj = Mapping.objects.get(ip_or_cname = ip)
            objs = Mapping.objects.filter(ip_or_cname=ip)
            for obj in objs:
                subnet_model = Subnet.objects.get(subnet_name=str(obj.subnet_id))
                response.write(
                    "{0:<30}{1}".format(
                        reversed_ip + ".in-addr.arpa.", "IN\tPTR\t" + obj.hostname + "." + subnet_model.domain + ".\n"
                    )
                )
    return response
예제 #2
0
파일: views.py 프로젝트: rollerd/django-dns
def text_all(request, domain_name):
    """
    Displays data for selected domain in text format. Index redirects here.
    Collects all subnets in the given domain and return direct response with the data

    arguments:
        request: request object
        domain_name: domain name
    """
    response = HttpResponse(content_type="text/plain")
    subnet_name = Subnet.objects.filter(domain=domain_name)
    if not subnet_name:
        response.write("ERROR: NO DOMAIN WITH NAME: %s FOUND" % domain_name)
    else:
        header = pylib.get_header(domain_name + ".")
        response.write(header)
        for obj in subnet_name:
            mapping = Mapping.objects.filter(subnet_id=obj.subnet_name)
            for record in mapping:
                response.write("{0:<30}{1}{2}\n".format(record.hostname, "IN\t%s\t" % record.record_type, record))
    return response