viewing paste Unknown #48717 | Python

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
# /usr/bin/env python2
# coding: utf-8
 
"""
Solution from:
<name 1> (<matriculation 1>)
<name 2> (<matriculation 2>)
"""
 
 
def ex06_task():
    """
    Your task is to use DNSSEC to walk the `example.com.` DNS zone. For each domain encountered, print the third level label.
    You do not need to import any modules. You can query any name server with the
        `query_nameserver(ip, port, domain, rdtype)`
    function defined below. The exact specification for the `rdtype` parameter can found in the dnspython documentation.
 
    A third level label looks like this:
        XXX.example.com
 
    A correct solution will be obvious.
    """
 
    # HINT:
    # You can extract the relevant part of the responses like this (for any valid i)
    # query_nameserver( ... )[i].next.to_text()
 
    # TODO implement
    domain = 'example.com.'
 
    while 1:
        domain = query_nameserver('139.19.117.15', 53535, domain, 'NSEC')[0].next.to_text()
        if domain == 'example.com.':
            break
        print domain.split('.')[0]
    pass
 
########################################################################################################################
# DO NOT MODIFY ANYTHING BELOW THIS LINE
########################################################################################################################
 
 
def query_nameserver(ip, port, domain, rdtype):
    """
    Queries the name server at ip:port for a certain domain and resource type.
 
    :param ip: str, IPv4 in dotted notation of the name server
    :param port: int, Port of the name server
    :param domain: str, The domain you want the resource records from.
    :param rdtype: int or str, The type of resource records. Examples: 1, 'A'
    :return: List of answers with matching resource record.
    """
 
    import dns.resolver
 
    r = dns.resolver.Resolver()
    r.nameservers = [ ip ]
    r.port = port
    try:
        tmp = r.query(domain, rdtype=rdtype, raise_on_no_answer=False)
        return [x for x in tmp.rrset] + [x for x in tmp.response.authority]
    except (dns.resolver.NXDOMAIN, TypeError) as e:
        return []
 
 
if __name__ == '__main__':
    ex06_task()
 
Viewed 1135 times, submitted by Guest.